473,387 Members | 3,033 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,387 software developers and data experts.

Is there a global validate on a winform?

I know how to use the ErrorProvider in my winforms..or do I? I validate
the values and set the ErrorProvider in the validating event. If not
valid, I set e.Cancel = True. I clear the ErrorProvider in the
validated event.

Is there a way to know if all validated controls pass validation when
the user clicks an OK button? In ASP.Net there's the Page.IsValid
method. Is there something similar in winforms, or do I still have to
write an overall validate routine to fire on the button click? I tried
Me.Validate(), but that always returned true...I guess I have to put my
final validations in the forms validate event?

It seems to me that form validation is much easier in ASP.Net apps.

Oct 6 '06 #1
11 11709
JJButer,

The difference is that the use of a Winform is different than from a
WebForm. A webform behaves as a classic simple non intelligent workstation.

It sends all data, that is validated and if it is wrong a message is send
back.

In winforms is the usage that every single control is checked and that the
validating is done on that, this makes it in most siutations more pleasant
for the users because as they have filled all the boxes they are ready and
normally no error cannot be throwed anymore. There can be some (business)
rules however that can mean that this is done because only the overal view
on the data can be checked.

You can simulate this behaviour on a webpage with JavaScript, but never as
fine as with a winform.

I hope that this gives an idea,

Cor

<jj******@hotmail.comschreef in bericht
news:11**********************@m73g2000cwd.googlegr oups.com...
>I know how to use the ErrorProvider in my winforms..or do I? I validate
the values and set the ErrorProvider in the validating event. If not
valid, I set e.Cancel = True. I clear the ErrorProvider in the
validated event.

Is there a way to know if all validated controls pass validation when
the user clicks an OK button? In ASP.Net there's the Page.IsValid
method. Is there something similar in winforms, or do I still have to
write an overall validate routine to fire on the button click? I tried
Me.Validate(), but that always returned true...I guess I have to put my
final validations in the forms validate event?

It seems to me that form validation is much easier in ASP.Net apps.

Oct 6 '06 #2
JJ,

Before I forget, one of the reasons to use Ajax is to get the winforms
behaviour on a webpage.

Cor

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:ep**************@TK2MSFTNGP04.phx.gbl...
JJButer,

The difference is that the use of a Winform is different than from a
WebForm. A webform behaves as a classic simple non intelligent
workstation.

It sends all data, that is validated and if it is wrong a message is send
back.

In winforms is the usage that every single control is checked and that the
validating is done on that, this makes it in most siutations more pleasant
for the users because as they have filled all the boxes they are ready and
normally no error cannot be throwed anymore. There can be some (business)
rules however that can mean that this is done because only the overal view
on the data can be checked.

You can simulate this behaviour on a webpage with JavaScript, but never as
fine as with a winform.

I hope that this gives an idea,

Cor

<jj******@hotmail.comschreef in bericht
news:11**********************@m73g2000cwd.googlegr oups.com...
>>I know how to use the ErrorProvider in my winforms..or do I? I validate
the values and set the ErrorProvider in the validating event. If not
valid, I set e.Cancel = True. I clear the ErrorProvider in the
validated event.

Is there a way to know if all validated controls pass validation when
the user clicks an OK button? In ASP.Net there's the Page.IsValid
method. Is there something similar in winforms, or do I still have to
write an overall validate routine to fire on the button click? I tried
Me.Validate(), but that always returned true...I guess I have to put my
final validations in the forms validate event?

It seems to me that form validation is much easier in ASP.Net apps.


Oct 6 '06 #3

jj******@hotmail.com wrote:
I know how to use the ErrorProvider in my winforms..or do I? I validate
the values and set the ErrorProvider in the validating event. If not
valid, I set e.Cancel = True. I clear the ErrorProvider in the
validated event.

Is there a way to know if all validated controls pass validation when
the user clicks an OK button? In ASP.Net there's the Page.IsValid
method. Is there something similar in winforms, or do I still have to
write an overall validate routine to fire on the button click? I tried
Me.Validate(), but that always returned true...I guess I have to put my
final validations in the forms validate event?

It seems to me that form validation is much easier in ASP.Net apps.
Well, I haven't done much WebForms, but yes, in WinForms, the whole
validation thing is sort of broken. The way I got around it is like
this,

private void textBox1_Validating(object sender, System.CancelEventArgs
e)
{
e.Cancel = ValidatePhoneNumber(testBox1.Text);
}

private bool ValidatePhoneNumber(string text)
{
string message = "";
if (... some test ...)
{
message = "Phone number not valid.";
}
errorProvider1.SetError(message);
return message.Length == 0;
}

then, in the OK button click event:

private void okButton_Click(object sender, System.EventArgs e)
{
bool phoneNumberValid = ValidatePhoneNumber(textBox1.Text);
...
if (phoneNumberValid && ... && ... )
{
this.DialogResult = DialogResult.OK;
}
}

In other words... good old "roll your own": write separate routines
that you call from the "Validating" event handlers, then call all of
those when the OK button is clicked. The one important trick is that
you have to call each one separately, then evaluate the booleans
afterward, or use the non-shortcut & operator instead of &&.

Oct 6 '06 #4
Hi Bruce,

I use your method if I need to set textBox1.CausesValidation to false so that the user can tab out of the control even if it
contains invalid data. Validation in btnOK_Click will then require a call to the custom validation method as you've illustrated.

Normally, however, I leave CausesValidation as true on all validating controls and call ValidateChildren() in the btnOK_Click
method. It protects me against forgetting to validate controls, especially if I add new ones and don't realize that there is
validation logic in the btnOK_Click method to be added.

The one exception, of course, is controls that accept user input but do not have a CausesValidation property. Off the top of my
head I can't think of any other than custom controls so it's not common to run into this issue. If it happens, just use a
combination of your code (explicitly calling the validation method for each offending control) and mine (calling ValidateChildren to
handle the rest).

private void text1_Validating(object sender, CancelEventArgs e)
{
e.Cancel = text1.Text != "text1";

if (e.Cancel)
errorProvider1.SetError(text1, "Value must be \"text1\"."); // normally, this text is extracted from a resource
else
errorProvider1.SetError(text1, null);
}

private void text2_Validating(object sender, CancelEventArgs e)
{
e.Cancel = text2.Text != "text2";

if (e.Cancel)
errorProvider1.SetError(text2, "Value must be \"text2\"."); // normally, this text is extracted from a resource
else
errorProvider1.SetError(text2, null);
}

private void btnOK_Click(object sender, EventArgs e)
{
if (ValidateChildren(ValidationConstraints.Visible | ValidationConstraints.Enabled
| ValidationConstraints.Selectable | ValidationConstraints.TabStop))
{
// form is valid
this.Close();
}
}

--
Dave Sexton

"Bruce Wood" <br*******@canada.comwrote in message news:11**********************@e3g2000cwe.googlegro ups.com...
>
jj******@hotmail.com wrote:
>I know how to use the ErrorProvider in my winforms..or do I? I validate
the values and set the ErrorProvider in the validating event. If not
valid, I set e.Cancel = True. I clear the ErrorProvider in the
validated event.

Is there a way to know if all validated controls pass validation when
the user clicks an OK button? In ASP.Net there's the Page.IsValid
method. Is there something similar in winforms, or do I still have to
write an overall validate routine to fire on the button click? I tried
Me.Validate(), but that always returned true...I guess I have to put my
final validations in the forms validate event?

It seems to me that form validation is much easier in ASP.Net apps.

Well, I haven't done much WebForms, but yes, in WinForms, the whole
validation thing is sort of broken. The way I got around it is like
this,

private void textBox1_Validating(object sender, System.CancelEventArgs
e)
{
e.Cancel = ValidatePhoneNumber(testBox1.Text);
}

private bool ValidatePhoneNumber(string text)
{
string message = "";
if (... some test ...)
{
message = "Phone number not valid.";
}
errorProvider1.SetError(message);
return message.Length == 0;
}

then, in the OK button click event:

private void okButton_Click(object sender, System.EventArgs e)
{
bool phoneNumberValid = ValidatePhoneNumber(textBox1.Text);
...
if (phoneNumberValid && ... && ... )
{
this.DialogResult = DialogResult.OK;
}
}

In other words... good old "roll your own": write separate routines
that you call from the "Validating" event handlers, then call all of
those when the OK button is clicked. The one important trick is that
you have to call each one separately, then evaluate the booleans
afterward, or use the non-shortcut & operator instead of &&.

Oct 6 '06 #5
That makes sense. However, you can't be assured that the user has
entered every textbox before using the mouse to click OK. So they all
have to be checked during the button click anyway. I just wish that
there was a Validate() method on controls that automatically forced the
validating event, and that would return True if e.Cancel = False and
False if e.Cancel = True. Then I wouldn't have to "double-up" my
validation calls. Then, if there was a Form.Validate() method, it could
fire the Validate() method of all its child controls and return the
same....similar to webforms.

Cor Ligthert [MVP] wrote:
JJButer,

The difference is that the use of a Winform is different than from a
WebForm. A webform behaves as a classic simple non intelligent workstation.

It sends all data, that is validated and if it is wrong a message is send
back.

In winforms is the usage that every single control is checked and that the
validating is done on that, this makes it in most siutations more pleasant
for the users because as they have filled all the boxes they are ready and
normally no error cannot be throwed anymore. There can be some (business)
rules however that can mean that this is done because only the overal view
on the data can be checked.

You can simulate this behaviour on a webpage with JavaScript, but never as
fine as with a winform.

I hope that this gives an idea,

Cor

<jj******@hotmail.comschreef in bericht
news:11**********************@m73g2000cwd.googlegr oups.com...
I know how to use the ErrorProvider in my winforms..or do I? I validate
the values and set the ErrorProvider in the validating event. If not
valid, I set e.Cancel = True. I clear the ErrorProvider in the
validated event.

Is there a way to know if all validated controls pass validation when
the user clicks an OK button? In ASP.Net there's the Page.IsValid
method. Is there something similar in winforms, or do I still have to
write an overall validate routine to fire on the button click? I tried
Me.Validate(), but that always returned true...I guess I have to put my
final validations in the forms validate event?

It seems to me that form validation is much easier in ASP.Net apps.
Oct 6 '06 #6
Hi,

Bruce Wood and I have addressed your concerns in our responses to your OP.

The Validate method will validate the control on which it's called and the ValidateChildren method will validate child controls.

--
Dave Sexton

<jj******@hotmail.comwrote in message news:11**********************@m7g2000cwm.googlegro ups.com...
That makes sense. However, you can't be assured that the user has
entered every textbox before using the mouse to click OK. So they all
have to be checked during the button click anyway. I just wish that
there was a Validate() method on controls that automatically forced the
validating event, and that would return True if e.Cancel = False and
False if e.Cancel = True. Then I wouldn't have to "double-up" my
validation calls. Then, if there was a Form.Validate() method, it could
fire the Validate() method of all its child controls and return the
same....similar to webforms.

Cor Ligthert [MVP] wrote:
>JJButer,

The difference is that the use of a Winform is different than from a
WebForm. A webform behaves as a classic simple non intelligent workstation.

It sends all data, that is validated and if it is wrong a message is send
back.

In winforms is the usage that every single control is checked and that the
validating is done on that, this makes it in most siutations more pleasant
for the users because as they have filled all the boxes they are ready and
normally no error cannot be throwed anymore. There can be some (business)
rules however that can mean that this is done because only the overal view
on the data can be checked.

You can simulate this behaviour on a webpage with JavaScript, but never as
fine as with a winform.

I hope that this gives an idea,

Cor

<jj******@hotmail.comschreef in bericht
news:11**********************@m73g2000cwd.googleg roups.com...
>I know how to use the ErrorProvider in my winforms..or do I? I validate
the values and set the ErrorProvider in the validating event. If not
valid, I set e.Cancel = True. I clear the ErrorProvider in the
validated event.

Is there a way to know if all validated controls pass validation when
the user clicks an OK button? In ASP.Net there's the Page.IsValid
method. Is there something similar in winforms, or do I still have to
write an overall validate routine to fire on the button click? I tried
Me.Validate(), but that always returned true...I guess I have to put my
final validations in the forms validate event?

It seems to me that form validation is much easier in ASP.Net apps.

Oct 6 '06 #7
Hmmm, am I missing a thread, or is ValidateChildren in 2.0 only?
I went ahead and created a custom control that inherits Textbox and
added this simple method (sorry for the VB instead of c#):

Public Function Validate() As Boolean
Dim args As New System.ComponentModel.CancelEventArgs
MyBase.OnValidating(args)
Return Not args.Cancel
End Function

This gives me what I want. I think I'll add a property called something
like AllowExitOnFailedValidation that I can check so I could still fire
Validating but choose whether or not to set e.Cancel. That way the
error will be shown, but they could still leave the field. Sometimes
users may want to tab past a required field to fill out another part of
form first. In my OK click, I do the following:

Dim bValidated As Boolean = True
For Each control As Control In Me.Controls
If TypeOf (control) Is WindowsControlLibrary1.MyTextbox
Then
If Not CType(control,
WindowsControlLibrary1.MyTextbox).Validate() Then
bValidated = False
End If
End If
Next
If Not bValidated Then
MsgBox("Errors found")
Else
MsgBox("OK")
End If

Oct 6 '06 #8
Hi,
Hmmm, am I missing a thread,
Do you mean that you can't see our posts? What news reader are you using?
or is ValidateChildren in 2.0 only?
Nope.
I went ahead and created a custom control that inherits Textbox and
added this simple method (sorry for the VB instead of c#):
<snip code>
>
This gives me what I want. I think I'll add a property called something
like AllowExitOnFailedValidation that I can check so I could still fire
Validating but choose whether or not to set e.Cancel. That way the
error will be shown, but they could still leave the field. Sometimes
users may want to tab past a required field to fill out another part of
form first. In my OK click, I do the following:
<snip code>

I think Bruce Wood's post addresses your concerns. Just set CausesValidation = false on your control too so that the user can tab
through it without being forced to enter valid data.

--
Dave Sexton
Oct 6 '06 #9
In what class is Validate and ValidateChildren a member? In searching
the VS .Net 2003 help, there is no entry for ValidateChildren. What am
I missing here?

Dave Sexton wrote:
Hi,
Hmmm, am I missing a thread,

Do you mean that you can't see our posts? What news reader are you using?
or is ValidateChildren in 2.0 only?

Nope.
I went ahead and created a custom control that inherits Textbox and
added this simple method (sorry for the VB instead of c#):
<snip code>

This gives me what I want. I think I'll add a property called something
like AllowExitOnFailedValidation that I can check so I could still fire
Validating but choose whether or not to set e.Cancel. That way the
error will be shown, but they could still leave the field. Sometimes
users may want to tab past a required field to fill out another part of
form first. In my OK click, I do the following:
<snip code>

I think Bruce Wood's post addresses your concerns. Just set CausesValidation = false on your control too so that the user can tab
through it without being forced to enter valid data.

--
Dave Sexton
Oct 6 '06 #10
It does appear in msdn2's documentation for the ContainerControl class.
The documentation doesn't say so, but it appears to have been added in
..NET 2.0, which is why neither you nor I can find it in our docs for
..NET 1.1 (aka VS 2003).

jj******@hotmail.com wrote:
In what class is Validate and ValidateChildren a member? In searching
the VS .Net 2003 help, there is no entry for ValidateChildren. What am
I missing here?

Dave Sexton wrote:
Hi,
Hmmm, am I missing a thread,
Do you mean that you can't see our posts? What news reader are you using?
or is ValidateChildren in 2.0 only?
Nope.
I went ahead and created a custom control that inherits Textbox and
added this simple method (sorry for the VB instead of c#):
>
<snip code>
>
This gives me what I want. I think I'll add a property called something
like AllowExitOnFailedValidation that I can check so I could still fire
Validating but choose whether or not to set e.Cancel. That way the
error will be shown, but they could still leave the field. Sometimes
users may want to tab past a required field to fill out another part of
form first. In my OK click, I do the following:
>
<snip code>

I think Bruce Wood's post addresses your concerns. Just set CausesValidation = false on your control too so that the user can tab
through it without being forced to enter valid data.

--
Dave Sexton
Oct 6 '06 #11
Hi Bruce,

Well that's just terrible. Thanks for the info and sorry for the confusion.

Hey, at least they addressed the issue in 2.0 :)

--
Dave Sexton

"Bruce Wood" <br*******@canada.comwrote in message news:11*********************@b28g2000cwb.googlegro ups.com...
It does appear in msdn2's documentation for the ContainerControl class.
The documentation doesn't say so, but it appears to have been added in
.NET 2.0, which is why neither you nor I can find it in our docs for
.NET 1.1 (aka VS 2003).

jj******@hotmail.com wrote:
>In what class is Validate and ValidateChildren a member? In searching
the VS .Net 2003 help, there is no entry for ValidateChildren. What am
I missing here?

Dave Sexton wrote:
Hi,

Hmmm, am I missing a thread,

Do you mean that you can't see our posts? What news reader are you using?

or is ValidateChildren in 2.0 only?

Nope.

I went ahead and created a custom control that inherits Textbox and
added this simple method (sorry for the VB instead of c#):

<snip code>

This gives me what I want. I think I'll add a property called something
like AllowExitOnFailedValidation that I can check so I could still fire
Validating but choose whether or not to set e.Cancel. That way the
error will be shown, but they could still leave the field. Sometimes
users may want to tab past a required field to fill out another part of
form first. In my OK click, I do the following:

<snip code>

I think Bruce Wood's post addresses your concerns. Just set CausesValidation = false on your control too so that the user can
tab
through it without being forced to enter valid data.

--
Dave Sexton

Oct 6 '06 #12

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

Similar topics

1
by: John Indra | last post by:
Hello all... I need to have global data available within the lifetime of my Winform app, accessible from any objects this application might spring at runtime. In ASP.NET, I can just create...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
7
by: Adam | last post by:
Im trying to add an httphandler for all *.sgf file extensions. I have developed the handler, 1. installed it into the gac 2. added it to the machine.config: <httpHandlers> <add verb="*"...
2
by: Kevin Yu | last post by:
hi all got a questoin here, I have a winform client to call web service and the web service functions have EnableSession =ture, use IE to test the web service session, it works, but how to get...
4
by: Water Cooler v2 | last post by:
I want to register a hotkey for my WinForm so that when I hold the, say, Alt+M keys, it minimizes. How do I do that?
1
by: Marek | last post by:
Hi all, who can give me an answer on this question: how to validate controls on WinForm? So clicking on AceptButton does not return control from WinForm to program until all controls have proper...
1
by: paulo | last post by:
Hello, I have a DLL library containing some web services which are declared in each .asmx file in the following way: <%@ WebService Language="C#" Class="LibraryName.WebService" %> I would...
3
by: Rodrigo Juarez | last post by:
Hi Microsoft Visual Studio 2005, Visual Basic and winforms application I need a global error handler, where should I put that. Any examples? TIA Rodrigo Juarez
2
karthickkuchanur
by: karthickkuchanur | last post by:
Iam developing visual studio window application i have lot o textbox in it how can i validate when the user enter the value,if it is in web form means i can easily validate using javascript.please...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.