Question error provider with multiple textboxes

mikewhorley

New member
Joined
May 29, 2011
Messages
3
Programming Experience
Beginner
I have a form with 20 textboxes that I would like to perform validation on. The validation rules of the textboxes are the same.

An example with 2 textboxes

VB.NET:
Private Sub txtName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles[B]TextBox1.Validating,TextBox2.Validating[/B]
If CType(sender, TextBox).Text = "" Then
ErrProvider.SetError(sender, "Must Be filled")
Else
ErrProvider.SetError(sender, "")
End If

This works but it means I have to list 20 textboxes in the handles part. Is there a better approach?
 
Ctrl-click to select all textboxes in designer (or rectangle select them if possible), in Properties window select Events view, select the common event handler for their Validating event. Designer then generated the Handles list for you.
 
Is there a better approach?

No there isn't. There are only two ways to handle events in VB.NET: AddHandler and Handles. What JohnH posted is the way it should be done but all that does is automatically add all the controls the Handles clause for you.
 
Back
Top