VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Numeric Style Textbox

by Chris Shell (4 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

From CShellVB https://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.

Inputs
NumberText as Text Box and Flag as Boolean
Assumes
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!
Code 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&

Rate Numeric Style Textbox

'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

Download this snippet    Add to My Saved Code

Numeric Style Textbox Comments

No comments have been posted about Numeric Style Textbox. Why not be the first to post a comment about Numeric Style Textbox.

Post your comment

Subject:
Message:
0/1000 characters