Write an event (not just add an event to the control)------------C #

Source: Internet
Author: User

Recent reading on the content of the event, before writing WinForm, events are directly used, the internal wording is not understood.

This will customize an event.

First, write a class.

  Public  classNewmaileventargs:eventargs//This class is later a parameter of the event that is used to pass some additional information about the event. But must inherit EventArgs    {        Private ReadOnly stringM_from, m_to, M_subject;  PublicNewmaileventargs (string  from,stringTo,stringsubject) {M_from= from; M_to=to ; M_subject=subject; }         Public stringfrom {Get{returnM_from;} }         Public stringto {Get{returnm_to;} }         Public stringSubject {Get{returnM_subject;} }    }

Write another class, which is something about the event.

  Public  classMailmanager { Public EventEventhandler<newmaileventargs> NewMail;//Define an event, in fact it is a paradigm delegate. This delegate requires two parameters. You can press F12 to follow the definition of this delegate

// Define an Event trigger method, here for type safety, claim protect protected Virtual voidOnnewmail (Newmaileventargs e)//The class defined above, used here as a parameter, to pass information{//because time can be called in many places, for type safety, here is not the direct manipulation of the NewMail eventeventhandler<newmaileventargs> temp = Volatile.read (refNewMail); if(Temp! =NULL) Temp ( This, E); } //exposes a method for converting input into an expected event, triggering an event Onnewmail Public voidSimulatenewmail (string from,stringTo,stringsubject) {Newmaileventargs e=NewNewmaileventargs ( from, To,subject); Onnewmail (e); } }

Here's the definition and triggering event.

 classProgram {Static voidMain (string[] args) {Mailmanager Mail=NewMailmanager (); Mail. NewMail+ = Mail_newmail;//Create EventMail. Simulatenewmail ("Hahah","HSHSHS","Ksksksk");//Trigger This eventMail. NewMail-= Mail_newmail;//Destroying Events        }        Static voidMail_newmail (Objectsender, Newmaileventargs e) {Console.WriteLine ("faxing mail message:"); Console.WriteLine ("from={0},to={1},subject={2}", E.from, e.to, E.subject);        Console.readkey (); }    }

Write an event (not just add an event to the control)------------C #

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.