» Selective Textboxes by Hrqls |
|
(Login to remove green text ads)
Ever had a textbox on your visual basic form, but only wanted to accept numeric data ?
Well, you can do that as follows :
To test the code: open a form, add a textbox to it, and put in the following code.
In the KeyPress event of the textbox put the following code
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 8 'Backspace is allowed
Case 44, 46 'Comma and dot are allowed
If InStr(Text1.Text, ".") Then 'When there is already a dot, then
KeyAscii = 0 'dont accept the dot
Else 'otherwise
KeyAscii = 46 'place a dot (also when the input was a comma)
End If
Case 48 To 57 'nunmbers are allowed
Case Else
KeyAscii = 0 'the rest is not accepted
End Select
End Sub
This code can simply be changed so it only accepts lowercase chars:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 8 'Backspace is allowed
Case 65 To 90 'Uppercase chars changed to lowercase
KeyAscii = KeyAscii - 32
Case 97 To 122 'Lowercase chars are allowed
Case Else
KeyAscii = 0 'the rest is not accepted
End Select
End Sub
As you can see all you have to do is to change the cases to make sure the right keys are allowed and the rest is ignored (or changed into something which is allowed).
|
|