Difference between revisions of "Number boxes"
From HashVB
(Number boxes in VB) |
m (Removed an erronious [[) |
||
Line 11: | Line 11: | ||
Const ES_NUMBER = &H2000& | Const ES_NUMBER = &H2000& | ||
− | Style = | + | Style = GetWindowLong(Text1.hWnd, GWL_STYLE) |
Style = Style or ES_NUMBER | Style = Style or ES_NUMBER | ||
SetWindowLong Text1.hWnd, GWL_STYLE, Style | SetWindowLong Text1.hWnd, GWL_STYLE, Style |
Revision as of 13:11, 23 August 2005
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.
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.