Validate Text box

CoachBarker

Well-known member
Joined
Jul 26, 2007
Messages
133
Programming Experience
1-3
How can I validate that only A,B,C,D are the only characters that are allowed to be typed into a text box?

Thanks
CoachBarker
 
In the keypress event of your text box do:

VB.NET:
If (e.KeyChar < "A" Or e.KeyChar > "D") Then
            e.Handled = True
End If
 
When I put that into the key-press event nothing happens at all when I tabbed into the textbox and try to type a letter.

Thanks
CoachBarker
 
When I put that into the key-press event nothing happens at all when I tabbed into the textbox and try to type a letter.

Thanks
CoachBarker

You'll have to be more specific than that. What do you mean "nothing happens at all"? This will prevent anything but A,B,C or D from being entered. Now that I think about it you should use this one instead so that you can use backspace and tab out of the box:

VB.NET:
If (e.KeyChar < "A" Or e.KeyChar > "D") Then
            If (AscW(e.KeyChar) <> AscW(ControlChars.Back) AndAlso AscW(e.KeyChar) <> AscW(ControlChars.Tab)) Then
                e.Handled = True
            End If
        End If
 
If I am adding a new record,
  • I tab into the text box
  • It already contains lets say C
  • The C is highlighted, but if I try to change it to one of the oter acceptable letters nothing happens at all. The C just stays highlighted

I will try your second post

Thanks
CoachBarker
 
Back
Top