Only allowing numbers into a text box
This is one of several ways of stopping a user enter letters into a text
box. It sets the window style for the text box, stopping it from accepting letters. This
could also box done in the KeyPress event of the text box, just intercepting letters, but
this way does not need code in the events of the text box.
Declarations
Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long Declare
Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Functions
Public Const GWL_STYLE = (-16)
Public Const ES_NUMBER = &H2000& Public Sub
SetNumber(NumberText As TextBox, Flag As Boolean)
Dim curstyle As Long
Dim newstyle As Long
curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)
If Flag Then
curstyle = curstyle Or ES_NUMBER
Else
curstyle = curstyle And (Not ES_NUMBER)
End If
newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
NumberText.Refresh
End Sub
|