Difference between revisions of "Number boxes"

From HashVB
Jump to: navigation, search
m (Removed an erronious [[)
m (Line wrapped the API calls)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{VB6}}
 
All the text boxes in windows have a special numeric mode.
 
All the text boxes in windows have a special numeric mode.
 
The description in MSDN is as follows:
 
The description in MSDN is as follows:
 
  Allows only digits to be entered into the edit control.
 
  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.
+
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 16: 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.