Search Tools Links Login

Numeric Style Textbox


Visual Basic 6, or VB Classic

From CShellVB http://www.cshellvb.com
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

Inputs

NumberText as Text Box and Flag as Boolean

Assumptions

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!

Returns

None.

API Declarations

'From CShellVB http://www.cshellvb.com
'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&

Code

'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
Else
  curstyle = curstyle And (Not ES_NUMBER)
End If
newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
NumberText.Refresh
End Sub

About this post

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

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.