Second form in my application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ronny

    Second form in my application

    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


  • Peter Morris

    #2
    Re: Second form in my application

    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(str ing newValue)
    {
    NewValue = newValue;
    }
    }

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

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

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




    Pete

    Comment

    Working...