In form1: A lable1 is used to accept the textbox1 information in form2, and button1 is used to show the form2
Code in form1:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace wintest
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
// When you click this button, the second form is displayed.
Form2 FM2 = new form2 ();
Fm2.myevent + = new form2.mydelegate (givevalue); // subscribe to the form2 event while showing the form, and call the givevalue () method.
Fm2.showdialog ();
}
Public void givevalue (string text) // Method for modifying the label
{
This. label1.text = text;
}
}
} In view code 1 form2: A textbox1 is used to enter the value to be passed, and button1 is used to trigger the transfer Event 1 form2 code:
2
3 using system;
4 using system. Collections. Generic;
5 using system. componentmodel;
6 using system. Data;
7 using system. drawing;
8 using system. text;
9 using system. Windows. forms;
10
11 namespace wintest
12 {
13 public partial class form2: Form
14 {
15 public delegate void mydelegate (string text); // define a delegate
16 public event mydelegate myevent; // defines events of the Appeal delegate type
17
18 public form2 ()
19 {
20 initializecomponent ();
21}
22 private void button#click (Object sender, eventargs E)
23 {
24 // trigger the event when you click the button on the form
25 if (myevent! = NULL)
26 {
27 myevent (textbox1.text );
28}
29}
30}
31} view code
Delegate value transfer