.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

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -