C #: Events

Source: Internet
Author: User

Event: an event is a message sent by an object, which notifies the customer of an operation. This operation may be caused by a mouse click or triggered by some other program logic. The sender of an event does not need to know which object or method receives the event triggered by the event. The sender only needs to know the intermediary (delegate) between the event and the receiver ).

Example 1:

1 using system; 2 using system. windows. forms; 3 4 namespace windowsformsapplication2 5 {6 public partial class form1: Form 7 {8 Public form1 () 9 {10 initializecomponent (); 11 12 // when a buttonone click event is triggered, the button_click method should be executed, and the new method should be added to the delegate list using the + = Operator 13 buttonone. click + = new eventhandler (button_click); 14 buttontwo. click + = new eventhandler (button_click); 15 buttontwo. click + = new eventhandler (button2_click); 16} 17 18 // delegate requires that all methods added to the delegate list must have the same signature 19 private void button_click (Object sender, eventargs e) 20 {21 if (button) sender ). name = "buttonone") 22 {23 labelinfo. TEXT = "button one was pressed. "; 24} 25 else26 {27 labelinfo. TEXT = "button two was pressed. "; 28} 29} 30 31 private void button2_click (Object sender, eventargs e) 32 {33 MessageBox. show ("this only happens in button two Click Event"); 34} 35} 36}

The event handler method has several important points:

  • The event handler always returns void, which cannot return values.
  • As long as eventhandler is used for delegation, the parameters should be object and eventargs. The first parameter is the object that triggers the event. In the preceding example, It is buttonone or buttontwo. The second parameter eventargs is an object that contains other useful information about the event. This parameter can be of any type as long as it is derived from eventargs.
  • You should also note that, according to the Conventions, the event handler should follow the naming conventions of "object_event.

If the lambda expression is used, the button_click method and the button2_click method are not required:

 1 using System; 2 using System.Windows.Forms; 3  4 namespace WindowsFormsApplication2 5 { 6     public partial class Form1 : Form 7     { 8         public Form1() 9         {10             InitializeComponent();11             buttonOne.Click += (sender, e) => labelInfo.Text = "Button one was pressed";12             buttonTwo.Click += (sender, e) => labelInfo.Text = "Button two was pressed";13             buttonTwo.Click += (sender, e) =>14                 {15                     MessageBox.Show("This only happens in Button2 click event");16                 };17         }18     }19 }

 

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.