Home
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
 
 
 
 
TitleMake a TextBox accept only digits
KeywordsTextBox, only digits, numeric field
CategoriesControls
 
Use the SetWindowLong API function to set the TextBox's extended style to include ES_NUMBER. Then subclass the control to monitor its messages.
 
Private Sub Form_Load()
Dim style As Long

    ' Get the current style.
    style = GetWindowLong(Text1.hWnd, GWL_STYLE)

    ' Add ES_NUMBER to the style.
    SetWindowLong Text1.hWnd, GWL_STYLE, style Or ES_NUMBER

    ' Subclass to ignore the context menu.
    OldWindowProc = SetWindowLong( _
        Text1.hWnd, GWL_WNDPROC, _
        AddressOf NewWindowProc)
End Sub
 
The new WindowProc ignores the WM_CONTEXTMENU message which drops down the control's context menu. Through that menu the user could paste non-digits into the TextBox.
 
' Pass along all messages except the one that makes the
' context menu appear.
Public Function NewWindowProc(ByVal hWnd As Long, ByVal msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
    If msg <> WM_CONTEXTMENU Then _
        NewWindowProc = CallWindowProc( _
            OldWindowProc, hWnd, msg, wParam, _
            lParam)
End Function
 
Note that my book Custom Controls Library shows how to make field controls that do this sort of thing.
 
 
Copyright © 1997-2001 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated