Search Tools Links Login

Numeric Style Textbox

From CShellVB
Change the style of a normal textbox so that it will only accept numbers. Better then evaluating keypress events and very fast. This call works on the fly and it is wrapped into a function for you.

Original Author: Chris


NumberText as Text Box and Flag as Boolean


This changes the style of the textbox for the life of the form. Even if the form or textbox is refreshed the style will remain in effect!



API Declarations

'From CShellVB
'Below is used for making a TextBox control accept
'only numbers. Very cool because it changes the style
'and does not require any code on the TextBox's events

'Place declares in Module
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
'Used for forcing only numbers in a textbox
Public Const GWL_STYLE = (-16)
Public Const ES_NUMBER = &H2000&


'This function changes the style based on the flag
Public Sub SetNumber(NumberText As TextBox, Flag As Boolean)
Dim curstyle As Long
Dim newstyle As Long
'This Function uses 2 API functions to set the style of
'a textbox so it will only accept numbers CShell
curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)
If Flag Then
  curstyle = curstyle Or ES_NUMBER
  curstyle = curstyle And (Not ES_NUMBER)
End If
newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 115 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.