c# - Validation - (Form) Cancel button doesnt work -


i having problems leaving form on cancelbutton_clicked event, because of validating events.

i have 1 textbox has own validating methods, , returns e.cancel = true if input string null or empty, else e.cancel = false.

now, have cancelbutton regular button, , close current form, this:

cancelbutton_clicked(object sender, eventargs e) { this.close(); } 

but if this, , if textbox left empty doesnt pass validation, , cant close form. validation icons keep blinking. tried setting causesvalidation false, tried this:

private void btncancel_click(object sender, eventargs e) {     // stop validation of controls form can close.     autovalidate = autovalidate.disable;     close(); } 

but none of helped. hope could. cheers

am assuming, have set btncancel.causesvalidation=false; either through code or designer.

setting causesvalidation=false of button, allow call click event of button

now there multiple things can do.

  1. simply unregister textbox validating events inside btn_cancel i.e.

    private void btncancel_click(object sender, eventargs e) {     textbox1.validating -= new canceleventhandler(textbox1_validating);     this.close(); } 
  2. simply use boolean flag. set true inside btncancel event , use inside validating event

    bool iscancelbtnclicked=false; private void btncancel_click(object sender, eventargs e) {     iscancelbtnclicked=true;     this.close(); } private void textbox1_validating(object sender, canceleventargs e) {     e.cancel=!iscancelbtnclicked; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -