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

Validating all controls on "Save"

P: n/a
I have a windows form that I want to force validation on controls (text
boxes) when the user clicks a "Save" button. The only way I've found to do
this is to cycle through every control and call it's .Select() method. This
is clunky though because you can see a flash in each text box as it's being
validated. Here's my code

Private Sub Save()
For each c as control in Me.Controls
If c.CanSelect() then
c.Select()
End if
Next c
End Sub

Each control has code in their Control_Validating event that fires off an
errorprovider.
Jul 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Ryan" <Ty****@newsgroups.nospamwrote in message
news:OH**************@TK2MSFTNGP04.phx.gbl...
I have a windows form that I want to force validation on controls (text
boxes) when the user clicks a "Save" button. The only way I've found to
do
this is to cycle through every control and call it's .Select() method.
This
is clunky though because you can see a flash in each text box as it's
being
validated. Here's my code

Private Sub Save()
For each c as control in Me.Controls
If c.CanSelect() then
c.Select()
End if
Next c
End Sub

Each control has code in their Control_Validating event that fires off an
errorprovider.


Jul 20 '06 #2

P: n/a

"Ryan" <Ty****@newsgroups.nospamwrote in message
news:OH**************@TK2MSFTNGP04.phx.gbl...
I have a windows form that I want to force validation on controls (text
boxes) when the user clicks a "Save" button. The only way I've found to
do
this is to cycle through every control and call it's .Select() method.
This
is clunky though because you can see a flash in each text box as it's
being
validated. Here's my code

Private Sub Save()
For each c as control in Me.Controls
If c.CanSelect() then
c.Select()
End if
Next c
End Sub

Each control has code in their Control_Validating event that fires off an
errorprovider.
Try using the LockWindowUpdate API call to prevent the objects from being
updated while you cycle through the list. This will prevent the "flash"
effect. If you hit an error, or you successfully reach the end of the list,
a call to LockWindowUpdate(0&) releases the lock. Since only one lock can be
in place at any one time you would want to lock the whole form during the
validation phase. (It doesn't cost anything extra.)

Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwnd As Long)
As Long

LockWindowUpdate(Me.Handle) ' Locks the form if "Me" is the form
LockWindowUpdate(0&) ' Unlocks whatever was locked previously

It doesn't stop the user from entering text but the added (or changed or
removed) text won't appear until after the unlock.
Jul 20 '06 #3

P: n/a
Hi Ryan,

Thank you for posting.

Have you set the CausesValidation property of the Save button to true? If
yes, when the focus is moved from one of these textboxes to the Save
button, the textbox's Validating event will be raised. In this case, you
needn't cycle through every control and call its Select method to force
validation.

However, if the user doesn't edit in any textbox on the form and click the
Save button directly, the textbox's Validating event won't be raised. In
this case, I think the simplest solution is to validate these textboxes in
the Save button's Click event handler, instead of validating the textboxes
in their Validating event handler. Of course, you could include the code of
validating these textboxes in a function and call this function in the Save
button's Click event handler.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 21 '06 #4

P: n/a
Ryan,

Why are you not adding the click event of your save button to all the
validating methods.

You can probably even do that in one time using the same routine you have
now, but use the validating method and add that handler to that (what I did
not try)

Cor

"Ryan" <Ty****@newsgroups.nospamschreef in bericht
news:OH**************@TK2MSFTNGP04.phx.gbl...
>I have a windows form that I want to force validation on controls (text
boxes) when the user clicks a "Save" button. The only way I've found to do
this is to cycle through every control and call it's .Select() method.
This is clunky though because you can see a flash in each text box as it's
being validated. Here's my code

Private Sub Save()
For each c as control in Me.Controls
If c.CanSelect() then
c.Select()
End if
Next c
End Sub

Each control has code in their Control_Validating event that fires off an
errorprovider.

Jul 21 '06 #5

P: n/a
Doh,

That probably won't go, the events have different event arguments. But you
can of course set in your Save Click event a routine that calls all the
validating methods you want.

Cor

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:uL**************@TK2MSFTNGP05.phx.gbl...
Ryan,

Why are you not adding the click event of your save button to all the
validating methods.

You can probably even do that in one time using the same routine you have
now, but use the validating method and add that handler to that (what I
did not try)

Cor

"Ryan" <Ty****@newsgroups.nospamschreef in bericht
news:OH**************@TK2MSFTNGP04.phx.gbl...
>>I have a windows form that I want to force validation on controls (text
boxes) when the user clicks a "Save" button. The only way I've found to
do this is to cycle through every control and call it's .Select() method.
This is clunky though because you can see a flash in each text box as it's
being validated. Here's my code

Private Sub Save()
For each c as control in Me.Controls
If c.CanSelect() then
c.Select()
End if
Next c
End Sub

Each control has code in their Control_Validating event that fires off an
errorprovider.


Jul 21 '06 #6

P: n/a
Ryan,
Are you using VS 2005 or an earlier version.

With VS 2005 you can use something like:

Private Sub buttonAccept_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles buttonAccept.Click
If Not ValidateChildren() Then
DialogResult = Windows.Forms.DialogResult.None
Return
End If
End Sub

Where you have AutoValidate set on the form itself:

Me.AutoValidate =
System.Windows.Forms.AutoValidate.EnableAllowFocus Change

And you have DialogResult set on the OK button itself.

buttonOk.DialogResult = System.Windows.Forms.DialogResult.OK

For earlier versions I use a loop similar to yours, however I use
Control.Focus & Form.Validate.

Private Sub buttonAccept_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles buttonAccept.Click
For Each control As control In Me.Controls
If control.CausesValidation Then
control.Focus()
If Not Me.Validate() Then
Me.DialogResult = DialogResult.None
Exit For
End If
End If
Next
End Sub

NOTE: This loop probably should be recursive to get controls within
container controls, within other container controls...

The above routine was adopted from Chris Sells' book "Windows Forms
Programming in C#" from Addison Wesley.

The "DialogResult = Windows.Forms.DialogResult.None" above prevents the
dialog box from closing & returning DialogResult.OK!

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Ryan" <Ty****@newsgroups.nospamwrote in message
news:OH**************@TK2MSFTNGP04.phx.gbl...
|I have a windows form that I want to force validation on controls (text
| boxes) when the user clicks a "Save" button. The only way I've found to
do
| this is to cycle through every control and call it's .Select() method.
This
| is clunky though because you can see a flash in each text box as it's
being
| validated. Here's my code
|
| Private Sub Save()
| For each c as control in Me.Controls
| If c.CanSelect() then
| c.Select()
| End if
| Next c
| End Sub
|
| Each control has code in their Control_Validating event that fires off an
| errorprovider.
|
|
Jul 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.