Access non-static field from static function [duplicate] - c#

This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 6 years ago.
I have two forms. Form1 has a label, Form2 has a button. I'm adding Form2 to Form1 as a control. When I click the button I want the label to update.
Code for Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
RunTest();
}
private void RunTest()
{
Form myForm2 = new Form2();
myForm2.TopLevel = false;
this.Controls.Add(myForm2);
myForm2.Show();
}
public static void UpdateLabel()
{
label1.Text = "Button Pressed"; //ERROR
}
}
Code for Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.UpdateLabel();
}
}
Calling the UpdateLabel() require it to be static, but then I can't update Label1.Text
Do you have any suggestions what I should do in this situation? I want to add many Form2 to Form1 when I get this to work.

In Form2 add a Property of Type Form1 and assign it with this from Form1.
private void RunTest()
{
Form myForm2 = new Form2();
myForm2.otherform = this; // <--- note this line
myForm2.TopLevel = false;
this.Controls.Add(myForm2); // TODO: why is this line here?
myForm2.Show();
}
You can then
private void button1_Click(object sender, EventArgs e)
{
otherform.UpdateLabel();
}
if you make UpdateLabel() non-static
public void UpdateLabel()
{
label1.Text = "Button Pressed";
}

Related

Changing TextBox Text of Form1 From Form2 in C#

I'm new in C# programming. I have a beginner level question:
How do I change the text property of the textbox1 in my form 2 object using a button in my form1?
Here's my code in form1:
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
This is in form2:
namespace DoubleForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = "Test";
}
}
}
When you add a text box or any control for that matter to a Winform using the controls toolbox the control gets added as private so it can't be accessed outside of the class it's created in. Easy enough to fix though just added a public property that lets you get and set the text box value as such
namespace DoubleForms
{
public partial class Form1 : Form
{
// NEW CODE
public string TextBoxText
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
Then from Form2 you can just call form1.TextBoxText = "blah blah" to set the value.
Code is creating new Form1 every-time you click the button, which is not you want I believe.
What you need to do is create an event in Form2 and then subscribe to that event in Form1, that way you can listen changes from Form2 and update Form1.
namespace DoubleForms
{
public partial class Form2 : Form
{
public event EventHandler Updated; // define an event handler
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(Updated != null)
{
Updated(sender, new EventArgs()); //Raise a change.
}
}
}
}
Now in Form1 subscribe to Form2 event.
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Updated += (se,ev)=> textBox1.Text = "Test"; // update textbox
frm2.Show();
}
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}

How to reLoad a form from another form

I have two forms, Form1 and Form2. I use ShowDialog() on Form2 from Form1. How can I run Form1Load() from Form2? Specifically, I want to refresh Form1 from Form2.
Firstly, make sure you assigning the Owner property on Form2 before showing it. This allows you to access the current instance of Form1.
class Form1 : Form
{
public void Method()
{
var form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}
}
From Form2 you can this use this.Owner to access the instance of Form1 and call any public methods or access any public properties. (Make sure the load event handler is public)
class Form2 : Form
{
public void Method()
{
this.Owner.form1_Load(null,null); //assuming you don't use these params.
}
}
In your form1_Load() I would recommend putting in a this.Refresh() to "refresh" the form. The refresh doesn't update some things that have data stored, it only repaints the form, so in the load event you will have to manually "refresh" things.
Create an instance of Form1 and then use the Refresh method or your Form1_Load method with that instance.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ShowMessage(string message)
{
MessageLabel.Text = message;
}
private void ShowForm2(object sender, EventArgs e)
{
Form2 Form2Copy = new Form2(this);
Form2Copy.ShowDialog();
}
}
and
public partial class Form2 : Form
{
Form1 Form1Copy;
public Form2(Form1 Parent)
{
InitializeComponent();
Form1Copy = Parent;
}
public void Button_Click(Object sender, EventArgs e)
{
Form1Copy.ShowMessage("Hello from Form2!");
}
}
Pass in Form1 to the ShowDialog() method of your Form2 instance:
private void Form1_Load(object sender, EventArgs e)
{
this.LoadEventCode();
}
public void LoadEventCode()
{
this.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass in Form1
}
Now over in Form2, cast the .Owner property to Form1 and do what you need:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.LoadEventCode();
}

Application Windows Form C# use menu (User control)

I have an application with 2 Forms, for those forms I have create a Menu which I depose on the two forms.
There is only a menuStrip item on the menu, I just want when I click on "test1" to redirect to Form1 and when I click to "test2" I want to redirect to Form2.
But if test1 is already open/display I don't want to show him again and the same for test2.
My code in my Menu :
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f2.Hide();
f1.Hide();
f1.ShowDialog();
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.Hide();
f2.Hide();
f2.ShowDialog();
}
}
My Form1 :
My Form2 :
I just want the same result like my Buttons in Form1 and Form2 :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f1 = new Form1();
f1.ShowDialog();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
I thought that the property Visible for forms could help me but not...
The problem is when I click on my buttons it's open a new window but when my form is already open I don't want to open it again.
Thanks for your reply, I hope that I am clear sorry for my english in advance.
You are currently creating a new form each time the click handler code is executed.
Here is one way, but its nasty and I wouldn't really recommend it. I've assumed that form1 is the entry to your application and that its also the exit of the application. This solution uses a singleton to hold the f1/f2 instances.
public static class Global
{
static Global()
{
f2 = new Form2();
}
public static Form f1;
public static Form f2;
}
Your menu altered:
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Global.f2.Hide();
Global.f1.Hide();
Global.f1.Show();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
Global.f1.Hide();
Global.f2.Hide();
Global.f2.Show();
}
public void SetForm1(Form form)
{
Global.f1 = form;
}
public void SetForm2(Form form)
{
Global.f2 = form;
}
}
And the forms:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Global.f1 = this;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Global.f2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Global.f1.Show();
}
}
Hope this helps.
Your logic is wrong.
It seems to me that you want to display different content depending on the user selection on that 'menuStrip'. You need to look at dynamic controls loading not different forms loading.
You can have just a MainForm with that 'menuStrip' and a Panel. Define some User Controls and dynamically add them to that panel based on the user selection.
Snippet
public MainForm : Form
{
public MainForm()
{
// code
}
public void MenuStrip_OptionSelected(object sender, EventArgs e)
{
Panel1.Controls.Clear();
switch(MenuStrip.SelectedValue)
{
case "UserControl1" : Panel1.Controls.Add(new UserControl1()); break;
...
}
}
}
public UserControl1 : UserControl
{
// code
}

working with a new Windows form [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a NewForm and I need it to do something in my main form when a button in my new form is clicked.
public Newform()
{
InitializeComponent();
}
private void cancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void dontsave_click(object sender, EventArgs e)
{
}
}
}
I have a dontsave button and I need it to clear my textbox in my mainform when clicked and close the newform.
This will get you started:
using System;
using System.Windows.Forms;
// Form1 code.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 frm2 = new Form2(); // Instantiate your form2 object.
public Form1()
{
InitializeComponent();
frm2.Show(); // Show the form.
}
private void button_save_Click(object sender, EventArgs e)
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.ShowDialog(); // This shows a 'Save' dialog.
if (saveDlg.ShowDialog() == DialogResult.OK) // Capture user input from the dialog.
{
// do some work here
}
}
private void dontsave_Click(object sender, EventArgs e)
{
frm2.ClearTextBox(frm2); // Call the 'ClearTextBox' function from form2.
}
private void cancel_Click(object sender, EventArgs e)
{
this.Close(); // NOTE: Probably better to use Application.Exit() here.
}
}
}
//Form2 code.
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void ClearTextBox(Form form) // Pass a form as an overload.
{
textBox1.Text = ""; // Clear the textbox.
}
}
}
when you create your NewForm, you need to either:
1) Create an overload constructor to accept your parent form
2) have a public property which holds a reference to your parent form then finally show your NewForm
then when you are pressing the "dontsave" - simply reference the parent form and clear the textbox, and making sure the textbox property is either public or much preferably, a method call (dont give full access to a UI Control from other forms!)
I strongly recommend against blindly passing one Form into the constructor of another. Instead, expose some property events of the child form:
public Form1() {
var childForm = new ChildForm();
childForm.DontSave += // event handler
}
class ChildForm : Form {
public event EventHandler DontSave {
add { dontSaveButton.Click += value; }
remove { dontSaveButton.Click -= value; }
}
}
At MainForm.cs
public partial class MainForm : Form
{
NewForm frm2;
public MainForm()
{
InitializeComponent();
frm2 = new NewForm();
frm2.Show();
frm2.dontSaveButton += new DontSaveButtonHandler(frm2_dontSaveButton);
}
void frm2_dontSaveButton()
{
textBox1.Clear();
frm2.Close();
}
}
At NewForm.cs
public delegate void DontSaveButtonHandler();
public partial class NewForm : Form
{
public event DontSaveButtonHandler dontSaveButton;
public NewForm()
{
InitializeComponent();
}
private void btnDontSave_Click(object sender, EventArgs e)
{
if (dontSaveButton != null)
{
dontSaveButton();
}
}
}
I would guess my answer is what supposed to be. Using delegate is a good practice.

Passing data between two forms with properties [duplicate]

This question already has answers here:
pass a value from one form to another
(7 answers)
Closed 4 years ago.
I am passing data between 2 windows forms in C#. Form1 is the main form, whose textbox will receive the text passed to it from form2_textbox & display it in its textbox (form1_textbox).
First, form1 opens, with an empty textbox and a button, on clicking on the form1_button, form2 opens. In Form2, I entered a text in form2_textbox & then clicked the button (form2_button).ON click event of this button, it will send the text to form1's textbox & form1 will come in focus with its empty form1_textbox with a text received from form2.
I am using properties to implement this task. FORM2.CS
public partial class Form2 : Form
{
//declare event in form 2
public event EventHandler SomeTextInSomeFormChanged;
public Form2()
{
InitializeComponent();
}
public string get_text_for_Form1
{
get { return form2_textBox1.Text; }
}
//On the button click event of form2, the text from form2 will be send to form1:
public void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.set_text_in_Form1 = get_text_for_Form1;
//if subscribers exists
if(SomeTextInSomeFormChanged != null)
{
SomeTextInSomeFormChanged(this, null);
}
}
}
FORM1.CS
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string set_text_in_Form1
{
set { form1_textBox1.Text = value; }
}
private void form1_button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged);
}
//in form 1 subcribe to event
Form2 form2 = new Form2();
public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e)
{
this.Focus();
}
}
In form2 you need to create event and subscribe to it in form1. Thats all.
//declare event in form 2
public event EventHandler SomeTextInSomeFormChanged;
// call event in form2 text_changed event
if(SomeTextInSomeFormChanged != null)
SomeTextInSomeFormChanged(this, null);
//in form 1 subcribe to event
var form2 = new Form2();
form2.SomeTextInSomeFormChanged += SomeHandlerInForm1WhereYouCanSetForcusInForm1
Update:
Form2:
public Form2()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
//if subscribers exists
if(SomeTextInSomeFormChanged != null)
{
SomeTextInSomeFormChanged(form2_textBox1, null);
}
}
Form1:
public partial class Form1 : Form {
public Form1() { InitializeComponent(); }
private void form1_button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged);
}
public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e)
{
var textBoxFromForm2 = (TextBox)sender;
form1_textBox1.Text = textBoxFromForm2.Text
this.Focus();
}
}
The website listed below has very good tutorials. This particular page demonstrates how this can be achieved:
http://www.vcskicks.com/data-between-forms.php
What about this.
((Form2)Application.OpenForms["Form2"]).textBox1.Text = "My Message";

Categories

Resources