Reputation: 148
How can I refresh an open form from another form?
For example:
Form 1
Label (Modifiers = Public)
Button (To show Form 2)
Form 2
Text Box (Enter value for Label and display it on label)
Button (Sends value to Label)
I've notice that after I entered value in text box, the label is not updating after I closed form 2.
Upvotes: 0
Views: 382
Reputation: 148
what if I use this method? Is there any disadvantage? or it's fine?
Form 1: Modifiers of label1 is Public
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 oForm = new Form2();
oForm.Owner = this;
oForm.Show();
}
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
(this.Owner as Form1).label1.Text = textBox1.Text;
}
}
Upvotes: 0
Reputation: 1323
For C# Winforms, this is how I'd do it.
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 oForm = new Form2();
oForm.ChangeLabelText += ChangeLabelText;
oForm.Show();
}
private void ChangeLabelText(object sender, EventArgs e)
{
string sText = sender as string;
label1.Text = sText;
}
Form 2:
public partial class Form2 : Form
{
public event EventHandler ChangeLabelText;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sText = textBox1.Text;
ChangeLabelText(sText, null);
}
}
Upvotes: 1