Number boxes

From HashVB
(Redirected from Numberboxes)
Jump to: navigation, search
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

All the text boxes in windows have a special numeric mode. The description in MSDN is as follows:

Allows only digits to be entered into the edit control.

Instead of having to faff around doing all sorts of validation and checks in the change and keypress events, just use this once on startup.

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

Const GWL_STYLE = (-16) 
Const ES_NUMBER = &H2000& 

Style = GetWindowLong(Text1.hWnd, GWL_STYLE)
Style = Style or ES_NUMBER
SetWindowLong Text1.hWnd, GWL_STYLE, Style

This will get the current textbox style, add the ES_NUMBER flag then set it back.

Please note that you SHOULDN'T use + to add flags together as it will go horribly wrong if it is already set.