In daily development, you often need to open a modal form, select an item in the modal form, and assign the ID or Name to the form that opens the modal form ......
In the Windows client, it is very simple to implement such a function, and the code will be very beautiful. Of course, this has something to do with personal writing.
There are two elegant methods, one is to use the interface to write the observer mode, and the other is to use the event Delegate. Now that you are playing on the Windows platform, use a more flexible event delegation method.
Let's look at the implementation code. (Red bold is the main important code)
Main Code of the modal form:
Public partial class SelectForm: Form
{
Public event Action <string> ReturnValue;
//..........
Private void button#click (object sender, EventArgs e)
{
If (ReturnValue! = Null) ReturnValue (this. textBox1.Text );
This. Close ();
}
}
Main Code for opening the form of the modal form:
Public partial class Form1: Form
{
//..........
Void button2_Click (object sender, EventArgs e)
{
SelectForm f = new SelectForm ();
F. ReturnValue + = (value) =>
{
This. textBox2.Text = value;
};
F. ShowDialog (this );
}
//..........
}