Implementing C # with delegates, passing values between forms and forms

Source: Internet
Author: User

Main interface:

Code in FORM1:

Namespace _06 Delegate Practice _ form Pass Value
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}


private void Btn1_click (object sender, EventArgs e)
{
Form2 f2=new Form2 (txt1. TEXT,DOSTH);//Pass a string and Dosth method
F2. Show ();
}


Assigning the value of a string variable to a text box
public void Dosth (String str)
{
This.txt1.Text = str;
}
}
}

Code in FORM2:

Namespace _06 Delegate Practice _ form Pass Value
{
public delegate void Mydel (string str);//define a delegate
public partial class Form2:form
{

Public Form2 ()
{
InitializeComponent ();
}


Private Mydel _mdl;//Instantiation of a delegate variable
Public Form2 (String Str,mydel mdl): This ()
{
This.txt2.Text = str;
THIS._MDL = MDL;
}


private void Btn2_click (object sender, EventArgs e)
{
if (this._mdl!=null)
{
THIS._MDL (txt2. Text);
This. Close ();
}
}
}
}

Implementing C # with delegates, passing values between forms and forms

Related Article

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.