473,734 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11762
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******@hotma il.comschreef in bericht
news:11******** **************@ m73g2000cwd.goo glegroups.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.nlschre ef in bericht
news:ep******** ******@TK2MSFTN GP04.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******@hotma il.comschreef in bericht
news:11******** **************@ m73g2000cwd.goo glegroups.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******@hotmai l.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_Valida ting(object sender, System.CancelEv entArgs
e)
{
e.Cancel = ValidatePhoneNu mber(testBox1.T ext);
}

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

then, in the OK button click event:

private void okButton_Click( object sender, System.EventArg s e)
{
bool phoneNumberVali d = ValidatePhoneNu mber(textBox1.T ext);
...
if (phoneNumberVal id && ... && ... )
{
this.DialogResu lt = 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.Causes Validation 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 CausesValidatio n as true on all validating controls and call ValidateChildre n() 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 CausesValidatio n 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 ValidateChildre n to
handle the rest).

private void text1_Validatin g(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_Validatin g(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(obj ect sender, EventArgs e)
{
if (ValidateChildr en(ValidationCo nstraints.Visib le | ValidationConst raints.Enabled
| ValidationConst raints.Selectab le | ValidationConst raints.TabStop) )
{
// form is valid
this.Close();
}
}

--
Dave Sexton

"Bruce Wood" <br*******@cana da.comwrote in message news:11******** **************@ e3g2000cwe.goog legroups.com...
>
jj******@hotmai l.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_Valida ting(object sender, System.CancelEv entArgs
e)
{
e.Cancel = ValidatePhoneNu mber(testBox1.T ext);
}

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

then, in the OK button click event:

private void okButton_Click( object sender, System.EventArg s e)
{
bool phoneNumberVali d = ValidatePhoneNu mber(textBox1.T ext);
...
if (phoneNumberVal id && ... && ... )
{
this.DialogResu lt = 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******@hotma il.comschreef in bericht
news:11******** **************@ m73g2000cwd.goo glegroups.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 ValidateChildre n method will validate child controls.

--
Dave Sexton

<jj******@hotma il.comwrote in message news:11******** **************@ m7g2000cwm.goog legroups.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******@hotm ail.comschreef in bericht
news:11******* *************** @m73g2000cwd.go oglegroups.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 ValidateChildre n 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.Componen tModel.CancelEv entArgs
MyBase.OnValida ting(args)
Return Not args.Cancel
End Function

This gives me what I want. I think I'll add a property called something
like AllowExitOnFail edValidation 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 WindowsControlL ibrary1.MyTextb ox
Then
If Not CType(control,
WindowsControlL ibrary1.MyTextb ox).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 ValidateChildre n 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 AllowExitOnFail edValidation 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 CausesValidatio n = 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 ValidateChildre n a member? In searching
the VS .Net 2003 help, there is no entry for ValidateChildre n. 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 ValidateChildre n 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 AllowExitOnFail edValidation 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 CausesValidatio n = 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

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

Similar topics

1
2165
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 this holder object, put this object in (System.Web.SessionState.HttpSessionState)Session object, and whenever I need to access the global data from virtually any object having access to the Session object, I just call
2
3496
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 which each page will use, but my question relates to how to use the database connection i create in all my classes. I have a database class, in a separate namespace and file, i created that handles all the connection opening, executing statements...
7
2917
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="*" path="*.sgf" type="CustomExtensionHandler, Extenders.CustomExtensionHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d831d925597c1031" validate="True"/> </httpHandlers>
2
6027
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 it to work in winform? it seems like every call to the service will start a new session even if the web service object is a global variable in the winform. kevin
4
1439
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
7334
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 values. TIA, MP
1
1734
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 like to log every unhandled exception that occurs on any web service declared in the library to a file. I noticed the global class, usually inside global.asax has the Application_Error event that can be
3
1549
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
1380
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 help me how can test it whether if correct or not and another thing id iam not want to validate the testbox indivitually please sent me the logic using looping condition or something else
0
8946
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, 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...
0
8776
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,...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
3
2180
bsmnconsultancy
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...

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.