Delegate value transfer

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.