/*
* Created by sharpdevelop.
* User: noo
* Date: 2009-8-17
* Time:
*
* Event 2
*/
Using system;
Using system. Windows. forms;
Class Test
{
Static void main () // entry function
{
Form FRM = new form (); // create a new form
FRM. Text = "My forms ";
Button btn1 = new button (); // create button 1
Btn1.text = "button 1 not clicked ";
Btn1.dock = dockstyle. Left;
Btn1.click + = new eventhandler (btn1_clicked); // Add the event processing proxy. btn1_clicked is the custom function name. When you click the button, run the code in btn1_clicked. This is the concept of proxy.
FRM. Controls. Add (btn1); // Add the button to the form
FRM. showdialog ();
}
Public static void btn1_clicked (Object sender, system. eventargs e) // The custom method of the proxy event, which is connected to the btn1.click event. Pay attention to the parameter writing method.
{
Button BTN = (button) sender; // sender is the event source control, and E is the event source data. Explicit conversion is used here.
BTN. Text = "click button 1 ";
Button btn2 = new button ();
Btn2.text = "button not clicked 2 ";
Btn2.dock = dockstyle. Right;
Btn2.click + = new eventhandler (btn2click );
System. Windows. Forms. Control C = BTN. parent; // Where BTN. parent gets the parent form of BTN, that is, the instantiated frm form.
C. Controls. Add (btn2); // Add the btn2 button to the frm form.
}
Public static void btn2click (Object sender, system. eventargs e) // The custom method of the proxy event, which is connected to btn2.click.
{
Button BTN = (button) sender;
BTN. Text = "click button 2 ";
Button btn3 = new button ();
Btn3.text = "New button 3 ";
Btn3.dock = dockstyle. Fill;
System. Windows. Forms. Control C = BTN. parent;
C. Controls. Add (btn3 );
}
} The running result is as follows: