CausesValidation on Cancel button

G

Guest

I have code in Validating events of my textboxes w/c do usual validation and e.Cancel = True when applicable. My problem is with the Cancel button (w/c has CausesValidation = False). If I close the form inside the Cancel button code (i.e., Me.Close), the Validating events of the textboxes fire, preventing the user from closing the form. I can think of many workarounds for this (such as introduce a form-level flag var) but I was hoping for a simpler solution. Thanks in advance =)
 
C

ClayB [Syncfusion]

Just something to try. Before you call Me.Close, try setting
Me.CausesValidation = False.

========================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools

jester said:
I have code in Validating events of my textboxes w/c do usual validation
and e.Cancel = True when applicable. My problem is with the Cancel button
(w/c has CausesValidation = False). If I close the form inside the Cancel
button code (i.e., Me.Close), the Validating events of the textboxes fire,
preventing the user from closing the form. I can think of many workarounds
for this (such as introduce a form-level flag var) but I was hoping for a
simpler solution. Thanks in advance =)
 
T

tonci.tomic

if you have following settings:

// <code>
this.cancel_btn.CausesValidation = false;
this.cancel_btn.DialogResult = DialogResult.Cancel;
// </code>

then, if you click on cancel button, dialog will be closed regardless
of validation.

You will stil have problem with ESC key, but that can be solved if you
override ProcessDialogKey this way:

// <code>
protected override bool ProcessDialogKey(Keys keyData) {
if(keyData == Keys.Escape) {
DialogResult = DialogResult.Cancel;
return true;
}
return base.ProcessDialogKey(keyData);
}
// </code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top