C #. Net event 2

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

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.