Question Textbox validation

JayWalker

Member
Joined
Jun 20, 2011
Messages
15
Programming Experience
Beginner
hello,

I need some suggestions on this one. Is there any way to validate textboxes?? Like for some, you can only insert numeric characters, for others only alphabetical characters? And I wanna validate em at da keypress event. I know how to do it using coding on ASCII keys. But can anyone give any other ideas, options, alternatives to do that?? :)

Thanks alot.
I.J
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
Depending on exactly what you're trying achieve, it may be harder than you think to do the job properly. One issue that people forget is pasting of invalid data. You might like to check this out:

Numeric Text Box

You can adapt the logic as required. The SimpleNumberBox would be a good place to start for non-numeric data.
 

JayWalker

Member
Joined
Jun 20, 2011
Messages
15
Programming Experience
Beginner
my solution :)

Thanks for ur info. the example program is helpful :)
btw this is the method I've mentioned earlier. about ASCII codes.


VB.NET:
Private Sub txtFindName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFindName.KeyPress

        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 92 Or Asc(e.KeyChar) > 122 Then
                e.Handled = True
            End If
        End If

    End Sub



97 to 122 - simple letters

65 to 90 - block letters

48 to 57 - numbers

8 - backspace

32 - space bar

13 - enter
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
If you're interested in filtering out specific characters in the KeyPress event then you should generally use members of the Char structure, e.g. Char.IsDigit, Char.IsLetter and Char.IsControl.
 

JayWalker

Member
Joined
Jun 20, 2011
Messages
15
Programming Experience
Beginner
I tried out it ur way y'day. It works fine except for a little problem. I wrote this code for the KeyPress event. The thing is, it fires (the msgbox pops up) when I enter the 2nd letter. When I enter the 1st one, it doesn't throw the error. any idea?? I tried it on KeyDown, GotFocus events. Didn't work.


VB.NET:
If Char.IsLetter(txtFindName.Text) Then
                     MsgBox("Doesn't accept")
                Exit Sub
End If
 

JayWalker

Member
Joined
Jun 20, 2011
Messages
15
Programming Experience
Beginner
ah no matter. I fixed it :) Wrote the following under the TextChanged event.


VB.NET:
If Char.IsLetter(txtFindName.Text) Then
            MsgBox("Doesn't accept")
            txtFindName.Text = ""
End If


This is easier than the ASCII thing, I must say. Thanks. cheers! :D

I.J
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
Don't use the TextChanged event. Use the KeyPress event. You just need to use it properly. For more info on keyboard events, follow the Blog link in my signature and check out my post on the subject. In this case, the character the user just entered is contained in the e.KeyChar property. It hasn't actually been added to the Text of the control at that stage, which is the whole point. If you handle the TextChanged event then it's too late. If you handle the KeyPress event then you can reject the character without changing the Text.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
This is from the SimpleNumberBox class:
    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim keyChar = e.KeyChar
        Dim formatInfo = NumberFormatInfo.CurrentInfo

        If Char.IsControl(keyChar) OrElse _
           Char.IsDigit(keyChar) OrElse _
           ((keyChar = formatInfo.NegativeSign OrElse _
             keyChar = formatInfo.NumberDecimalSeparator) AndAlso _
            Me.ValidateText(keyChar)) Then
            MyBase.OnKeyPress(e)
        Else
            e.Handled = True
        End If
    End Sub
This is inside the class itself but it's the same code because this OnKeyPress method is what raises the KeyPress event.
 
Top Bottom