Hello World!
I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters.
[CODE=net]
Public Enum MyOption
Alpha = 1
AlphaNumeric = 2
Numeric = 3
End Enum
Public Sub SetCharacter(By Val CharacterOption As MyOption, _
ByVal kp As KeyPressEventAr gs)
Select Case CharacterOption
Case MyOptions.Alpha
If Not ( _
kp.KeyChar Like "[A-Z]" Or _
kp.KeyChar Like "[a-z]" Or _
kp.KeyChar = vbBack Or _
Asc(kp.KeyChar) = 32) Then
kp.KeyChar = vbNullChar
End If
Case MyOptions.Alpha Numeric
If Not ( _
kp.KeyChar Like "[A-Z]" Or _
kp.KeyChar Like "[a-z]" Or _
kp.KeyChar Like "[ñÑ]" Or _
kp.KeyChar Like "[0-9]" Or _
kp.KeyChar = vbBack Or _
Asc(kp.KeyChar) = 32) Then
kp.KeyChar = vbNullChar
End If
Case MyOptions.Numer ic
If Not ( _
kp.KeyChar Like "[0-9]" Or _
kp.KeyChar = vbBack Or _
Asc(kp.KeyChar) = 32) Then
kp.KeyChar = vbNullChar
End If
End Select
End Sub[/CODE]
Just place the SetCharacter method to the KeyPress event of a textbox, combobox, etc.
Sample 1. A textbox that accepts only numeric characters
[CODE=vb6]Private Sub Text1_KeyPress( ByVal sender As Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles Text1.KeyPress
SetCharacter(My Option.Numeric, e)
End Sub[/CODE]
As you noticed, the SetCharacter method has 2 parameters-CharacterOption and kp.
CharacterOption - Sets the MyOption value whether you want alpha, alphanumeric, or numeric
kp - Gets the current key that you have pressed.
Rey Sean
Mabuhay ang pinoy : )
I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters.
[CODE=net]
Public Enum MyOption
Alpha = 1
AlphaNumeric = 2
Numeric = 3
End Enum
Public Sub SetCharacter(By Val CharacterOption As MyOption, _
ByVal kp As KeyPressEventAr gs)
Select Case CharacterOption
Case MyOptions.Alpha
If Not ( _
kp.KeyChar Like "[A-Z]" Or _
kp.KeyChar Like "[a-z]" Or _
kp.KeyChar = vbBack Or _
Asc(kp.KeyChar) = 32) Then
kp.KeyChar = vbNullChar
End If
Case MyOptions.Alpha Numeric
If Not ( _
kp.KeyChar Like "[A-Z]" Or _
kp.KeyChar Like "[a-z]" Or _
kp.KeyChar Like "[ñÑ]" Or _
kp.KeyChar Like "[0-9]" Or _
kp.KeyChar = vbBack Or _
Asc(kp.KeyChar) = 32) Then
kp.KeyChar = vbNullChar
End If
Case MyOptions.Numer ic
If Not ( _
kp.KeyChar Like "[0-9]" Or _
kp.KeyChar = vbBack Or _
Asc(kp.KeyChar) = 32) Then
kp.KeyChar = vbNullChar
End If
End Select
End Sub[/CODE]
Just place the SetCharacter method to the KeyPress event of a textbox, combobox, etc.
Sample 1. A textbox that accepts only numeric characters
[CODE=vb6]Private Sub Text1_KeyPress( ByVal sender As Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles Text1.KeyPress
SetCharacter(My Option.Numeric, e)
End Sub[/CODE]
As you noticed, the SetCharacter method has 2 parameters-CharacterOption and kp.
CharacterOption - Sets the MyOption value whether you want alpha, alphanumeric, or numeric
kp - Gets the current key that you have pressed.
Rey Sean
Mabuhay ang pinoy : )
Comment