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