In .NET, the form is also defined as a class, so to call a method in the form, in addition to setting the method to be called to public, you have to create an instance of a form class. However, in this case, when we turn on the form 1 (an instance of this form), then open the form 2, then we want to pass an event in the form 2. Call a method in the form 1, and then we can't create a new instance of the form, what should I do?
One of the solutions is to implement the System.EventHandler provided in .NET. Methods as below:
Create a WindowsApplication with two forms, add a Button in Form1 and Form2, let's add some code.
First, first add it in Form2.
Add a System.EventHandler in Form2
Publlic Event System.EventHandler CallMessage;
Tune OnClicks in the Button Click event in Form2
Private void Button1_Click (Object Sender, System.Eventargs E)
{
THIS.CALLMESSAGE (Sender, E);
}
Next, add it in Form1.
Add a method to Form1, the parameters of the method are essential
Public Void ShowMessage (Object Sender, Eventargs E)
{
MessageBox.show ("Another Form Call this Method!");
}
Create an instance of form2 in the Button's Click event in Form1, and join the implementation method of the CallMessage of Form2
Private void Button1_Click (Object Sender, System.Eventargs E)
{
FORM2 FRM = New Form2 ();
frm.callMessage = New EventHandler (ShowMessage);
Frm.showdialog ();
}
Ok, our purpose is reached.
It is simple to write, I hope to play the role of the brick.