Difference between revisions of "Number boxes"

From HashVB
Jump to: navigation, search
m (Clarified when to use the coede)
m (Line wrapped the API calls)
 
Line 6: Line 6:
 
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.
 
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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long,
  Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) 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 GWL_STYLE = (-16)  
Line 17: Line 19:
  
 
This will get the current textbox style, add the ES_NUMBER flag then set it back.
 
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.
 
Please note that you SHOULDN'T use + to add flags together as it will go horribly wrong if it is already set.

Latest revision as of 15:50, 19 March 2006

float
 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.