A bit of experience about customizing events

Source: Internet
Author: User
Today, when searching for an article on MSDN, I accidentally saw a part of the custom event involved, all day use of the basic Windows Development environment under the IDE's own events, such as Click What, for a while did not read, turned back to check the book, just a little understanding, and deepened the impression. Although previously used in Java in the event, but in C # events must first define the delegate, a relatively messy, tidy up the first.

First defines the function signature of an event (the format of the parameter)--Proxy
public delegate void Computesalarycallback (string empid,double Salary);

Declare the event name contained in a type in a type, and the logic that is triggered by the event
public event Computesalarycallback Oncomputesalarydone;

In a function, declare the logic of the event trigger, in other words, trigger the Oncomputesalarydone event when the following function is invoked:
Public virtual double computesalary ()
{oncomputesalary (mstrempid,mdblbasesalary);
return mdblbasesalary;
}

Define the event handler function and connect the event to the event handler function, which is like delegating to that function
public static void Salaryhander (String empid,double Salary)
{
Console.weiteline ("Employee: Employee Code ={0}, Salary ={1}", empid,salary);
}

Oncomputesalarydone+=new Computesalarycallback (Salaryhandler);


The above completes the whole of an event, and once the Computesalary () function is invoked, the event Oncomputesalarydone is triggered and the event handler is raised. If you connect to more than one event handler function, the connection's time processing function is executed sequentially.

In addition, you can place the event definition in the component, and then declare the event handler function in the other class to handle it.


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.