Validate Multiple Text Boxes?

  • Thread starter Thread starter B-Dog
  • Start date Start date
B

B-Dog

I have a form that has about 10 text boxes on it, they all have to be filled
out before submitting is there a quick way to make sure that none are null
or do I have to call out each textbox? Say something like textbox1 through
textbox10? Thanks
 
Create a handler for the first textbox by double clicking on it,

Use the error provider and validated event. See the documentation for
examples.




--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Sorry, scrub the first line of my post above.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Thanks, OHM. I see if I can find an example.

One Handed Man ( OHM - Terry Burns ) said:
Sorry, scrub the first line of my post above.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
 
B-Dog,
In addition to adding handlers for the Validating event that Terry (OHM)
suggested.

I got the following tip from "Windows Forms Programming in C#" by Chris
Sells, from Addison Wesley.

Within your "Accept" button click handler (the "save" button) process each
control that CausesValidation to ensure that they are all valid...

Something like:

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

Note this version does not validate controls nested within other container
controls, such as GroupBoxes...

The above code will cause the Validating event for each of your controls to
be raised, ensuring that all the controls get validated, before the dialog
is closed or the data is saved...

Hope this helps
Jay
 
I couldn't find exactly how to do it, I'm very new at VB but this is how I
did it temporarily until I figured out the other way. I heard using the
validated event was the way to go cause it would show an icon next to the
field that needs attention or something. I just don't know how to implememt
it. I have textbox2 - 8

check to make sure all is filled out

Dim c As Control

For Each c In Me.Controls

If TypeOf c Is TextBox Then

If CType(c, TextBox).Text = "" Then

Exit Sub

End If

End If

Next
 
B-Dog said:
I couldn't find exactly how to do it, I'm very new at VB but this is how I
did it temporarily until I figured out the other way. I heard using the
validated event was the way to go cause it would show an icon next to the
field that needs attention or something. I just don't know how to implememt
it. I have textbox2 - 8

check to make sure all is filled out

Dim c As Control

For Each c In Me.Controls

If TypeOf c Is TextBox Then

If CType(c, TextBox).Text = "" Then

Exit Sub

End If

End If

Next
 
B-Dog,
Its "easier" to use the Validating event for TextBox, Validating is
inherited from Control, so all controls have a Validating event.

For details on the Validating event see:

http://msdn.microsoft.com/library/d...emwindowsformscontrolclassvalidatingtopic.asp

In your case you can use something like for all 8 text boxes:

Private Sub textBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles
textBox1.Validating

If textBox1.Text = "" Then
' Cancel the event and select the text to be corrected by the user.
e.Cancel = True
End If
End Sub

Alternatively you can have all 8 use one routine.

Private Sub textBox_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles
textBox1.Validating, TextBox2.Validating, TextBox3.Validating,
TextBox4.Validating

Dim txt As TextBox = DirectCast(sender, TextBox)

If txt.Text = "" Then
' Cancel the event and select the text to be corrected by the user.
e.Cancel = True
End If
End Sub



Note the above link using the ErrorProvider control to display errors to the
user.

http://msdn.microsoft.com/library/d...SystemWindowsFormsErrorProviderClassTopic.asp

Hope this helps
Jay
 
Here is the pattern I follow...

Drag an ErrorProvider control from toolbox onto form. (this gives you the
red icon beside the control(s) that is not valid)


' form level variable
Private _error1 As Boolean

Private Sub txtLName_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles txtLName.Validating
Validate_LName()
End Sub

Private Sub Validate_LName()
If txtLName.Text = "" Then
ErrorProvider1.SetError(txtLName, "Required field")
_error1 = True

Else
ErrorProvider1.SetError(txtLName, "")
' don't set _error1=false, cause other validators may have set
it somewhere else...
End If
End Sub

'^^^ repeat for each control you want to validate

Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAccept.Click
Dim myNewhire As New ePCO.NewHireDetails

' assume everything on form is Ok...
_error1 = False

' validate everything again (some control may have never gotten
focus)
Validate_LName()
'^^^repeat for each control you want to validate

'if found an error abort...
If _error1 Then Exit Sub

' save...
End Sub

HTH,
Greg
 
Thanks, guys that worked good, like the error provider, just what I was
looking for, cool feature.
 
Back
Top