By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,569 Members | 1,630 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,569 IT Pros & Developers. It's quick & easy.

Validation and cancelling click events in WinForms

P: n/a
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?
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


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

"Marina" <so*****@nospam.com> wrote in message
news:uK*************@TK2MSFTNGP15.phx.gbl...
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?

Nov 21 '05 #2

P: n/a
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.

--
Dennis in Houston
"Marina" wrote:
To be more specific, how to do this on controls that don't have a
CausesValidation property?

"Marina" <so*****@nospam.com> wrote in message
news:uK*************@TK2MSFTNGP15.phx.gbl...
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?


Nov 21 '05 #3

P: n/a
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...

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
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.

--
Dennis in Houston
"Marina" wrote:
To be more specific, how to do this on controls that don't have a
CausesValidation property?

"Marina" <so*****@nospam.com> wrote in message
news:uK*************@TK2MSFTNGP15.phx.gbl...
> 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?
>


Nov 21 '05 #4

P: n/a
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

"Marina" <so*****@nospam.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP12.phx.gbl...
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...

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
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.

--
Dennis in Houston
"Marina" wrote:
To be more specific, how to do this on controls that don't have a
CausesValidation property?

"Marina" <so*****@nospam.com> wrote in message
news:uK*************@TK2MSFTNGP15.phx.gbl...
> 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?
>


Nov 21 '05 #5

P: n/a
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.

"Francisco Garcia" <no****@nowhere.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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

"Marina" <so*****@nospam.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP12.phx.gbl...
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...

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
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.

--
Dennis in Houston
"Marina" wrote:

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

"Marina" <so*****@nospam.com> wrote in message
news:uK*************@TK2MSFTNGP15.phx.gbl...
> 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?
>



Nov 21 '05 #6

P: n/a
On 2005-09-29, Marina <so*****@nospam.com> wrote:
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.


"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
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.

--
Dennis in Houston
"Marina" wrote:
To be more specific, how to do this on controls that don't have a
CausesValidation property?

"Marina" <so*****@nospam.com> wrote in message
news:uK*************@TK2MSFTNGP15.phx.gbl...
> 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?
>


Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.