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.
|
|