In-depth understanding of Components in C # programming-events-Delegation

Source: Internet
Author: User

The understanding of events in component programming is very important. The "events" in C # is a way that classes provide notifications to customers of this class when an object has something interesting. In my opinion, delegate-delegate can encapsulate method references in the delegate object. In order to understand the relationship between component-event-delegate, I will discuss the younger brother's understanding with practical examples.
First, create a Windows Control Project and add the following control template.

When an event is triggered, an EventArgs type parameter is passed to the event processing method. To pass custom information, we can create an event parameter class inherited from EventArgs, it is defined as follows:
Public class EventLoginArgs: System. EventArgs
{
Public string strUserID;
Public string strUserName;
Public string strUserPWD;
Public bool bVaild;
Public EventLoginArgs (string userID, string userName, string userPWD)
{
StrUserID = userID;
StrUserName = userName;
StrUserPWD = userPWD;
}
Two more delegates are declared, which encapsulate the information in the EventLoginArgs and EventArgs objects, as follows:
Public delegate void UserLoginEventHandler (object sender, EventLoginArgs e );
Public delegate void CancelEventHandler (object sender, EventArgs e );
To allow users to customize the processing method of an event, the component must provide an event interface. if you only inherit from a single existing Windows control, you can reload known methods to add your own processing, or declare custom event interfaces. if the component contains multiple controls, you should declare the event interface as needed. Here, I declare two custom event interfaces for the use of the two buttons, as shown below:
Public event UserLoginEventHandler SubmitLogin;
Public event CancelEventHandler Cancel;
Protected virtual void OnSubmitLogin (EventLoginArgs e)
{
If (this. SubmitLogin! = Null)
{
SubmitLogin (this, e );
}
}

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.