Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / September 2005

Tip: Looking for answers? Try searching our database.

Validation and cancelling click events in WinForms

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marina - 28 Sep 2005 20:31 GMT
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?
Marina - 28 Sep 2005 20:45 GMT
To be more specific, how to do this on controls that don't have a
CausesValidation property?

> Imagine a form with some fields and an OK buttons that saves the
> information. Each field has validation logic in the Validating event.  If
[quoted text clipped - 20 lines]
>
> It seems like a pretty common scenario? How do people deal with this?
Dennis - 29 Sep 2005 02:01 GMT
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.

Signature

Dennis in Houston

> To be more specific, how to do this on controls that don't have a
> CausesValidation property?
[quoted text clipped - 23 lines]
> >
> > It seems like a pretty common scenario? How do people deal with this?
Marina - 29 Sep 2005 14:03 GMT
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...

>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
[quoted text clipped - 36 lines]
>> >
>> > It seems like a pretty common scenario? How do people deal with this?
Francisco Garcia - 29 Sep 2005 14:21 GMT
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

> 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
[quoted text clipped - 41 lines]
>>> >
>>> > It seems like a pretty common scenario? How do people deal with this?
Marina - 29 Sep 2005 18:01 GMT
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.

> Hi,
>
[quoted text clipped - 55 lines]
>>>> >
>>>> > It seems like a pretty common scenario? How do people deal with this?
david - 30 Sep 2005 04:30 GMT
> 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.

>>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
[quoted text clipped - 36 lines]
>>> >
>>> > It seems like a pretty common scenario? How do people deal with this?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.