Second form in my application

  • Thread starter Thread starter Ronny
  • Start date Start date
R

Ronny

I have a second winform in my application. I need to modify the text of a
text box in the mainform from this second form.
Is that possible and how?
Regards
Ronny
 
The best approach is probably to have an event

A: An event on your 2nd form which you trigger whenever you need to notify
the first form of the new value

public delegate void MyEventHandler(sender object, MyEventArgs args);
public class MyEventArgs : EventArgs
{
public readonly string NewValue;
public MyEventArgs(string newValue)
{
NewValue = newValue;
}
}

public class Form2 : Form
{
....etc.....
public event MyEventHandler MyEvent;
protected void OnMyEvent(string newValue)
{
if (MyEvent != null)
MyEvent(this, new MyEventArgs(newValue));
}
}

When you create the 2nd form you do this....

var f2 = new Form2();
f2.MyEvent += (sender, args) => TextBox1.Text = args.NewValue;
f2.Show();




Pete
 
Back
Top