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

Is there a global validate on a winform?

P: n/a
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
Share this Question
Share on Google+
11 Replies


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

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

P: n/a

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

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

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

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.