C # simple event example

Source: Internet
Author: User
Simple event example

Using system. windows, form; public class form1: FORM {Public Delegate void actioneventhandler (Object sender, actioncanceleventargs eV); // define the delegate type actioneventhandler public static event actioneventhandler action; // define the event busentity = new busentity (); public form () {initializecomponent () ;}/ * Public void form1_load () {actioncanceleventargs E = new actioncanceleventargs (); action + = new actioneventhandl Er (onaction); // trigger event} */public static void onaction (Object sender, actioncanceleventargs eV) {If (action! = NULL) {Action (sender, Ev) ;}} private void btnraise_click (Object sender, eventagrs e) {actioncanceleventargs cancelevent = new actioncanceleventargs (); onaction (this, cancelevent ); if (cancelevent. cancel) {lblinfo. TEXT = cancelevent. message;} else {lblinfo. TEXT = busentity. timestring ;}} public class actioncanceleventargs: system. componentmodel. canceleventargs {Public String message {Get; set;} public Actioncanceleventargs (): This (false) {}// constructor public actioncanceleventargs (bool cancel): This (false, "") {}// constructor public actioncanceleventargs (bool cancel, string message): Base (cancel) // constructor {This. message = message ;}}/* The New actioncanceleventargs Class Based on eventargs is derived from canceleventargs and canceleventargs is derived from eventargs. Canceleventargs adds the cancel attribute, which is a Boolean value and notifies the senser object that the receiver wants to cancel or stop event processing. The message attribute is added to actioncanceleventargs. */Using system; namespace test {public class busentity {string time = ""; Public busentity () {form1.action + = new form1.actioneventhandler (form1_action);} private void form1_action (Object sender, actioncanceleventargs e) {e. cancel =! Doactions (); If (E. cancel) {e. message = "wasn't the right time" ;}} private bool doactions () {bool retval = false; datetime TM = datetime. now; If (TM. second <30) {time = "the tine is" + datetime. now. tolongtimestring (); retval = true;} else {time = "": Return retval ;}} Public String timestring {get {return time ;}}}}

 

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.