Validation and cancelling click events in WinForms

M

Marina

Imagine a form with some fields and an OK buttons that saves the
information. Each field has validation logic in the Validating event. If
the input is not valid, the control's value is replaced with the last value
it had before the user changed it. Pretty typical

Now, the user types some invalid value and clicks OK. The user gets a
message saying the value is not valid, and the bad value is replaced with
the last good one. So far, so good.

But, now the button's Click event fires, and the form is saved and closed.

Now, the user, would not have wanted to save the form with the old value,
since clearly they were trying to update a field in the form.

So, if the validation was not valid, we essentially want to cancel the Click
event of the button.

Now, as forms and scenarios get more complex, it becomes necessary to deal
with this issue in a more generic manner. However, it seems that doing
something like is far from easy. It can probably be done by mucking around
with timers and boolean values, but surely there must be some other built in
way to deal with this problem in windows forms?

It seems like a pretty common scenario? How do people deal with this?
 
M

Marina

To be more specific, how to do this on controls that don't have a
CausesValidation property?
 
G

Guest

I have a form Control type variable (acceptok) that I set to = the control
causing the error in the validation or lostfocus event then in the OK button
click, check if acceptok is not nothing then set the focus to the control to
which the acceptok varialbe was set.
 
M

Marina

Right, that might work on a small scale. The issue being that this is for a
generic framework, and it is not known ahead of time what controls will be
on the form. So this needs to happen without having to write special code
for each form...
 
F

Francisco Garcia

Hi,

Just set the Cancel property of the Validating event arguments (of type
CancelEventArgs) to true.

It will prevent your control from losing focus, and any further processing
(like the button getting focus and being clicked) will be canceled.

Hope it helps
 
M

Marina

Validating does not get called in this case at all. That is my whole
problem. Sorry, but did you read my post?

Clicking on some controls does not raise the validation events. Such as
those that do not support the CausesValidation property.
 
D

david

Right, that might work on a small scale. The issue being that this is for a
generic framework, and it is not known ahead of time what controls will be
on the form. So this needs to happen without having to write special code
for each form...

My general solution to this is to have each data item (not the control,
but the object that drives it) register to a standard validation class.
A click on the ok button only submits if all registered items have
indicated that they are now valid.

Often I prefer not to enable the OK button until the form has validated,
but that just doesn't work correctly with data binding or validation
events.
 

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