How can I update a label on one form from another form in C#?
I want to update label of Form1
from Form2
. So here's what I have:
// Form1
public string Labe开发者_Go百科l1
{
get { return this.label1.Text; }
set { this.label1.Text = value; }
}
// Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Label1 = this.textBox1.Text;
this.Close();
}
So the above code doesn't work. However, when I add the following:
frm1.Show();
after
this.Close();
in Form2 code, the Form1 obviously is being opened again (two windows). But I want it to update in the same window so I suggest this.Close()
is unnecessary.
Anyone have any ideas?
In the button1_Click
method, you're actually creating a new instance of Form1
and setting the Label1
property of that new instance. That explains why a second instance of Form1
is being shown on the screen when you add frm1.Show();
. If you look at that second copy carefully, you'll see that it's label displays the correct value, but your original copy of that form retains its original value.
Instead, I assume that what you want to do is set the Label1
property for the existing instance of Form1
that is already displayed on the screen. In order to do that, you're going to have to get a reference to that existing form object.
Thus, this becomes a simple question of "how do I pass data between two objects (forms)", which has been asked countless times here on Stack Overflow. See the answers to these related questions:
- Communicate between two windows forms in C#
- Passing values between two windows forms
- C# beginner help, How do I pass a value from a child back to the parent form?
- Send values from one form to another form in c# winforms application
Try using singleton design pattern for that as I think the problem is when you using new
public class Form1Singleton {
private static final Form1Singleton INSTANCE = null;
// Private constructor prevents instantiation from other classes
private Form1Singleton () {
}
public static Form1Singleton getInstance()
{
if(INSTANCE ==null)
INSTANCE =new Form1Singleton();
return INSTANCE;
}
}
Now you can use the form easily only 1 form
private void button1_Click(object sender, EventArgs e)
{
Form1Singleton frm1 = Form1Singleton.getInstance();
frm1.Label1 = this.textBox1.Text;
this.Close();
}
I hope that solve your problem
BR,
Mohammed Thabet Zaky
Most probably: What you actually do is create a brand new instance of Form1
within the scope of Form2
, while later in the control flow of your program another instance of Form1
is created and shown. Of course, in such a case you can't expect your label to be set correctly.
The best would be to decouple Form1
and Form2
from each other and instead pass only data along the control flow of your program.
When you say this.close that means all the values you set to that instance gets nullifies.
You can use delegate here, to set form values.
MVP pattern is good for this type of senario and also separate UI and business logic.
This thread is good reference: UI Design Pattern for Windows Forms (like MVVM for WPF)
精彩评论