.net - How does System.ComponentModel.CancelEventHandler work? I dont understand what a cancelable event is? -


i strated developing forms in windows forms(c#) , having difficulty in understanding how system.componentmodel.canceleventhandler works , cancelable event means.

this.txtname.validating += new        system.componentmodel.canceleventhandler(this.txtboxempty_validating); this.txtaddress.validating += new        system.componentmodel.canceleventhandler(this.txtboxempty_validating); 

i appreciate if can explain event handling.

the implementation of textbox's validating event go like:

string previoustext = this.text; string textthatwasentered = this.getinput();  // update text inputted value this.text = textthatwasentered;  // validate inputted text, giving handlers chance cancel canceleventargs canceleventargs = new canceleventargs(); // call handlers, passing canceleventargs can set .cancel if needed this.onvalidating(this, canceleventargs);   // if cancelled, reset text previous value if (canceleventargs.cancel) {    this.text = previoustext; } 

so, it's way stop control updating. use email address validator:

void txtemail_validating(object sender, canceleventargs e) {    string inputtedtext = this.txtemail.text;    if (!inputtedtext.contains("@")) {       // no @ sign - deny update       e.cancel = true;       messagebox.show("invalid email address");    } } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -