PDA

Click to See Complete Forum and Search --> : How can i only let #'s in a textbox?


XxEvilxX
Dec 5th, 1999, 10:07 AM
How can i only let #'s in a textbox?

Ruchi
Dec 5th, 1999, 10:48 AM
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

QWERTY
Dec 5th, 1999, 11:20 AM
or try this:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End Sub


or if you want to have Backspace enabled then use this:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub

It should do the job

------------------
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).]

jim
Dec 5th, 1999, 09:57 PM
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)

Desire
Dec 6th, 1999, 01:20 AM
Try using the password char option in the textbox properties

QWERTY
Dec 6th, 1999, 04:54 AM
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.