Add a Class To your Project
name it myCon or Whatever
Assume that your main form name is Form1
Place Two TextBoxes on it
textBox1 and textBox2
textBox1 must only accept Alphabets
textBox2 must only accept Numbers
add the following Code On your Class :
Public Sub txtValidation(ByVal e)
If (Asc(e.KeyChar)) < 65 Or (Asc(e.KeyChar)) > 90 And (Asc(e.KeyChar)) < 97 Or (Asc(e.KeyChar)) > 122 Then
If (Asc(e.KeyChar) <> 32) Then
e.Handled = True
End If
End If
If (Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub
the above Code will Restrict The user to input
any other text except alphabets.Next for Numbers only, add the Following to your Class:
Public Sub txtNumberValidation(ByVal e)
If (Asc(e.KeyChar) < 48) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub
After that Add a module,name it whatever and to create the object of your Class.Declare here the following Code to your module,so that you can access it anywhere in your project :
Public valid As myCon
And Finally move on to your main form Containing Textboxes
and on there keypress Event put the following Code to call the Class and the Function for the Validation
Private Sub textBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox1.KeyPress
valid = New myCon
valid.txtValidation(e)
End Sub
Private Sub textBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox2.KeyPress
valid = New myCon
valid.txtNumberValidation(e)
End Sub
That's all Run it ,learn it. These things are something about OOPS.for more Info Click here[
^] Have Fun..