How can i only let #'s in a textbox?
Printable View
How can i only let #'s in a textbox?
Private Sub Text1_KeyPress(KeyAscii As Integer)
If ((KeyAscii >= vbKey0) And (KeyAscii <= vbKey9)) Or KeyAscii = vbKeyBack Then
Exit Sub
Else
KeyAscii = 0
End If
End Sub
Ruchi
or try this:
or if you want to have Backspace enabled then use this:Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End Sub
It should do the jobCode:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
------------------
Visual Basic Programmer (at least I want to be one)
------------------
PolComSoft
You will hear a lot about it.
[This message has been edited by QWERTY (edited 12-06-1999).]
Or for lots of fun, try using the API to do it:
Private Const GWL_STYLE = (-16)
Private Const ES_NUMBER = &H2000
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
' Put this code in your [Form_Load] Event
Dim Old_Style As Long
Dim New_Style As Long
'
Old_Style = GetWindowLong(Text1.hwnd, GWL_STYLE)
New_Style = SetWindowLong(Text1.hwnd, GWL_STYLE, Old_Style Or ES_NUMBER)
Try using the password char option in the textbox properties
PasswordChar wouldn't work, because it changes only appearance of the TextBox, it doesn't change its contest.
------------------
Visual Basic Programmer (at least I want to be one)
------------------
PolComSoft
You will hear a lot about it.