Ryan,
Are you using VS 2005 or an earlier version.
With VS 2005 you can use something like:
Private Sub buttonAccept_Cl ick(ByVal sender As Object, ByVal e As
EventArgs) Handles buttonAccept.Cl ick
If Not ValidateChildre n() Then
DialogResult = Windows.Forms.D ialogResult.Non e
Return
End If
End Sub
Where you have AutoValidate set on the form itself:
Me.AutoValidate =
System.Windows. Forms.AutoValid ate.EnableAllow FocusChange
And you have DialogResult set on the OK button itself.
buttonOk.Dialog Result = System.Windows. Forms.DialogRes ult.OK
For earlier versions I use a loop similar to yours, however I use
Control.Focus & Form.Validate.
Private Sub buttonAccept_Cl ick(ByVal sender As Object, ByVal e As
EventArgs) Handles buttonAccept.Cl ick
For Each control As control In Me.Controls
If control.CausesV alidation Then
control.Focus()
If Not Me.Validate() Then
Me.DialogResult = DialogResult.No ne
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 "DialogResu lt = Windows.Forms.D ialogResult.Non e" 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****@newsgro ups.nospamwrote in message
news:OH******** ******@TK2MSFTN GP04.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_Validat ing event that fires off an
| errorprovider.
|
|