Update TextBox from second Form
-
hi, I have and application which has 2 forms. The firts form has a textbox and a button which clicked opens a second form, which has just one button which clicked calls a method in the first form. The problem is, that the textbox doesn't update. There are no excpetion, when I set a break point the program goes into the line which updates the textbox but nothing happens. What is wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}public void add() { textBox1.Text = "Test"; } private void button1\_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } }
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Form1 f1 = new Form1(); f1.add(); } }
-
hi, I have and application which has 2 forms. The firts form has a textbox and a button which clicked opens a second form, which has just one button which clicked calls a method in the first form. The problem is, that the textbox doesn't update. There are no excpetion, when I set a break point the program goes into the line which updates the textbox but nothing happens. What is wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}public void add() { textBox1.Text = "Test"; } private void button1\_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } }
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Form1 f1 = new Form1(); f1.add(); } }
You are instantianting a new Form1, this is not the existing Form1 that opened your second form. When the first form opens the second, it should provide a reference to itself that you can then use in the second form. Optionally, you can check the Owner/ParentForm properties or something like that, depending on how you show the second form. public partial class Form2 : Form { private Form1 _parentForm; public Form2() { InitializeComponent(); } public Form2(Form1 parentForm) { InitializeComponent(); this._parentForm = parentForm; } private void button1_Click(object sender, EventArgs e) { if (this._parentForm != null) { this._parentForm.Add(); } } In this example, your Form1 code is supposed to do something like: Form2 form = new Form2(this); form.Show();
Jean-Christophe Grégoire
-
hi, I have and application which has 2 forms. The firts form has a textbox and a button which clicked opens a second form, which has just one button which clicked calls a method in the first form. The problem is, that the textbox doesn't update. There are no excpetion, when I set a break point the program goes into the line which updates the textbox but nothing happens. What is wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}public void add() { textBox1.Text = "Test"; } private void button1\_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } }
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Form1 f1 = new Form1(); f1.add(); } }
This should really be done with a delegate/event. I prefer using events for stuff like this. Form2 needs a new event that it can raise, and make you Form1 instance subscribe to it after it has instanciated it. If using an event you'll need a class derived from EventArgs to pass the data back. This is the sort of idea.
// Form1
using System;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.ReadyToUpdate += new EventHandler<Form2.MyEventArgs>(frm2\_ReadyToUpdate); frm2.Show(); } void frm2\_ReadyToUpdate(object sender, Form2.MyEventArgs e) { textBox1.Text = e.Text; } }
}
//Form2
using System;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public event EventHandler<MyEventArgs> ReadyToUpdate;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OnReadyToUpdate(new MyEventArgs("Test"));
Close();
}
protected virtual void OnReadyToUpdate(MyEventArgs e)
{
EventHandler<MyEventArgs> eh = ReadyToUpdate;
if (eh != null)
eh(this, e);
}
public class MyEventArgs : EventArgs
{
public MyEventArgs(string text)
{
m_Text = text;
}
private string m_Text;
public string Text
{
get { return m_Text; }
}
}
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
You are instantianting a new Form1, this is not the existing Form1 that opened your second form. When the first form opens the second, it should provide a reference to itself that you can then use in the second form. Optionally, you can check the Owner/ParentForm properties or something like that, depending on how you show the second form. public partial class Form2 : Form { private Form1 _parentForm; public Form2() { InitializeComponent(); } public Form2(Form1 parentForm) { InitializeComponent(); this._parentForm = parentForm; } private void button1_Click(object sender, EventArgs e) { if (this._parentForm != null) { this._parentForm.Add(); } } In this example, your Form1 code is supposed to do something like: Form2 form = new Form2(this); form.Show();
Jean-Christophe Grégoire
Jean-Christophe Grégoire wrote:
it should provide a reference to itself
Although this works and is a common method - it should not be encouraged. Form2 shouldn't know anything about Form1 and no method on it should be tied to an instance of a control that it hasn't created itself. If it needs to broadcast information, it should use an event/delegate - in the same way that every other control does :)
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Jean-Christophe Grégoire wrote:
it should provide a reference to itself
Although this works and is a common method - it should not be encouraged. Form2 shouldn't know anything about Form1 and no method on it should be tied to an instance of a control that it hasn't created itself. If it needs to broadcast information, it should use an event/delegate - in the same way that every other control does :)
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)I certainly do agree, I didn't want to bother him ;-)
Jean-Christophe Grégoire