Second form in my application

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
 
P

Peter Morris

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Modifying a ready application 2
Modify button name in Winform 1
Importing form GUI from another project 3
4 panes WinForm 2
MSMQ 3
Can't track an exception 3
thread communication 9
Winfrom and WEB (not web-form). 2

Top