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? 7 2777
To be more specific, how to do this on controls that don't have a
CausesValidatio n property?
"Marina" <so*****@nospam .com> wrote in message
news:uK******** *****@TK2MSFTNG P15.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?
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_Valida ting(object sender, System.Componen tModel.CancelEv entArgs 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.
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 CausesValidatio n property?
"Marina" <so*****@nospam .com> wrote in message news:uK******** *****@TK2MSFTNG P15.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?
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****@discuss ions.microsoft. com> wrote in message
news:BB******** *************** ***********@mic rosoft.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 CausesValidatio n property?
"Marina" <so*****@nospam .com> wrote in message news:uK******** *****@TK2MSFTNG P15.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? >
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******** ********@TK2MSF TNGP12.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****@discuss ions.microsoft. com> wrote in message news:BB******** *************** ***********@mic rosoft.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 CausesValidatio n property?
"Marina" <so*****@nospam .com> wrote in message news:uK******** *****@TK2MSFTNG P15.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? >
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 CausesValidatio n property.
"Francisco Garcia" <no****@nowhere .com> wrote in message
news:%2******** *******@tk2msft ngp13.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******** ********@TK2MSF TNGP12.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****@discuss ions.microsoft. com> wrote in message news:BB******** *************** ***********@mic rosoft.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 CausesValidatio n property?
"Marina" <so*****@nospam .com> wrote in message news:uK******** *****@TK2MSFTNG P15.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? >
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****@discuss ions.microsoft. com> wrote in message news:BB******** *************** ***********@mic rosoft.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 CausesValidatio n property?
"Marina" <so*****@nospam .com> wrote in message news:uK******** *****@TK2MSFTNG P15.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? >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
One of the main problems at this point lies in cancelling the event.
I have found that the TAB fires the onkeypress in NN, but not in IE.
I can cancel the onkeypress fine in NN. The TAB fires the onkeydown
in IE and can be cancelled in IE. ...
|
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 see) are
mostly annoying and sometimes even harmful.
How does knowing a control was clicked by the mouse (and how many times it
was clicked) help a client object know what the user intended to do by that
action?
How many times have you found...
|
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.
This form does things like setup event handlers on each control so that
|
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 project. The framework is built on
a modified Page Controller pattern in which a aspx page controls the loading
of clients (user controls) into a placeholder (Sort of similar to .net 2.0
Master Pages except that the page loads the client not the client...
|
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 appear to
be the result of double-clicking the submit button on the form.
The form has three ASP:Button tags, each of which gets translated into
INPUT TYPE="SUBMIT" HTML elements. One submits the form's data. One
logs the user out. And the other...
| |
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 textbox from the Validating event.
Now put the focus back on the textbox click the button bar button.
Nothing happens.
The form causes validation is true, so is the button bar and the
control.
|
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 the OK button set as the AcceptButton and the Cancel button as the
.... well, you've guessed it.
I click OK and the form closes, without going to the Form1_Validating event.
I could validate in the Click event of the OK button, but I can't...
|
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 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...
|
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 behaviour of course. However, he also wants this same behaviour when
hitting ESC on the keyboard.
It was my understanding that when you assign a button to a form's
CancelButton property you automatically gained this functionality. However,
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |