Reputation: 1849
My TextBox
and Label
Text
property does not update when I call a method from another Form
?
Here is the code
//Form1 Codes
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
Form2 frm= new Form2 ();
frm.UpdateText(this.treeView1.SelectedNode.Text.ToString());
this.Close();
}
//Form2 codes
//show Form1
private void Button1_Click(object sender, EventArgs e)
{
Form1 Frm = new Form1();
Frm.ShowDialog();
}
//update Textbox and lable
public void UpdateText(string text)
{
this.label1.Text = text;
textBox1.Text = text;
label1.Refresh();
}
Thanks in advance.
Upvotes: 0
Views: 354
Reputation: 236228
You are creating new instance of Form2 (which in not visible to client, because you don't show it) and updating it's label. What you need is updating label on existing instance of Form2. So, you need to pass instance of Form2 to Form1, which you create in Button1_Click event handler. Or (the better way) you need to define property on Form1 and read that property when Form1 is closed:
Form1 code
public string SelectedValue
{
get { return treeView1.SelectedNode.Text; }
}
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
// this means that user clicked on tree view
DialogResult = DialogResult.OK;
Close();
}
Form2 code
private void Button1_Click(object sender, EventArgs e)
{
using(Form1 frm1 = new Form1())
{
if(frm1.ShowDialog() != DialogResult.OK)
return;
UpdateText(frm1.SelectedValue);
}
}
public void UpdateText(string text)
{
label1.Text = text;
textBox1.Text = text;
label1.Refresh();
}
Upvotes: 2