473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Validation and cancelling click events in WinForms

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?
Sep 28 '05 #1
7 2749
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?

Sep 28 '05 #2
Digit
4
I was wondering how this worked and did a small test :
I created a form with only a textbox and a button, which closes the form.
I made the button the acceptbutton and filled in some stupid code in the "validating" event :
private void textBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (textBox2.Text!="Hallo")
e.Cancel=true;
textBox2.Text="After Cancel";
}

When I click on the button or press return in the textbox with "Test" in it, nothing happens (I stay focused in the textbox). I can't even close the form with the red cross (upper right). Even Alt+F4 doesn't help. The tekst in the textbox has also changed into "After cancel".

So I guess that you only have to set e.Cancel on true.
Sep 28 '05 #3
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?


Sep 29 '05 #4
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?
>


Sep 29 '05 #5
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?
>


Sep 29 '05 #6
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?
>



Sep 29 '05 #7
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?
>


Sep 30 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul THompson | last post by:
I have been working for some time to 1) detect tab and shift tab events 2) control the focus on the basis of these events. I have found that I can do this, but continue to have nagging problems. ...
0
by: Yoni Rapoport | last post by:
Hy everyone, just wanted to share a thought regarding UI. If you really think about it, events such as "Click", "DoubleClick", "KeyDown", "KeyUp", etc... (which clutter every winforms control we...
6
by: PromisedOyster | last post by:
Hi How do I cancel an event in a class where that event is setup in another derived class. See example below. Thanks All our winforms are derived from one of our own classes, BaseForm....
4
by: AIM48 | last post by:
Hi. We have a framework that we work with for our project. So far we have had very good success – basically the frame work wraps many day to day tasks so that they are all included in the...
1
by: rmgalante | last post by:
I have written an ASP.Net application that uses the standard client-side and server-side validation for various fields on the form. Some of the customers that use the form report symptoms that...
2
by: Tim Frawley | last post by:
Source code attached indicates my problem with validation and a button bar save button. Fill the Textbox with some text then tab off the control. The message box will display the text in the...
22
by: Charles Law | last post by:
Could someone please explain to me, in words of one syllable or less, how I get the Validating event to fire for a form. I have a form with one text box, and two buttons: OK and Cancel. I have...
6
by: Marina | last post by:
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...
5
by: Micky | last post by:
VB v7.1.3088 NET v1.1.4322 SP1 My mate has a strange problem regarding the ESC key and validation. When he hits the Cancel button on his form, the form does not validate. This is correct...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.