How do you use the api to force text boxes to only accept numerical values. Also only numbers not decimal points etc. These numbers will be ints or longs but not float or double.
Printable View
How do you use the api to force text boxes to only accept numerical values. Also only numbers not decimal points etc. These numbers will be ints or longs but not float or double.
You need to add the ES_NUMBER style.
Code:Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const ES_NUMBER = &H2000&
Private Const GWL_STYLE = (-16)
Private Sub Command1_Click()
Dim lStyle As Long
lStyle = GetWindowLong(Text1.hwnd, GWL_STYLE) Or ES_NUMBER
SetWindowLong Text1.hwnd, GWL_STYLE, lStyle
End Sub
Also, here's a non-API method, if you like:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (Chr(KeyAscii) Like "#") Then KeyAscii = 0
End Sub