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 ;}}}}