How to stop a control from causing validation

M

Matt Taylor

On my form I have text box which I need to validate against incorrect
user input.

This is done using the Validating event of the textbox. If the input is
incorrect I'll show a message, set e.Cancel = True and set focus back to
the textbox.

I also have a Cancel button. If the textbox has the focus when this
button is clicked the form should close wihout worrying about the
contents of the textbox.

Without a CausesValidation property to play with I'm not sure how I can
prevent the validation from firing when the button is clicked.

I had the same issue with the form close 'OK' button but managed to get
around this by setting my own CancelValidation variable to True in the
from.Close event and then checking this variable in the
textbox.Validating event (as recommended by other posts).

This only works however because the form.Close event seems to fire
before the textbox.Validating event.
This is not the case for the Cancel button as no event seems to fire
before the textbox.Validating event so I can't set the CancelValidation
variable to True.

Does anybody know if this is a recognised problem and if there is
something I can do to prevent the Cancel button from causing validation?

Thanks,

Matt

Aplologies if this post is duplicated, Google groups was messing me
about.
 

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