Search Tools Links Login

Force Numeric Entry in a Text Box Using API


Visual Basic 6, or VB Classic

This routine will cause a textbox control to accept only numeric input. Any other input (including the '-' and '.' keys) will be ignored. Note that it is still possible to paste non-numeric data into the textbox. There have been plenty of examples of this in the last couple of days - in the comments section of one of these someone suggested using the API. So, for anyone that is interested, this is one way of doing it...
I've put this in the intermediate category only because it uses API functions - otherwise it's straightforward code. I've only tested this in VB6.0 on Win2k, but it should work on any Windows platform from Win95 up.

Original Author: Dion Campbell

API Declarations

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const ES_NUMBER = &H2000
Private Const GWL_STYLE = -16

Code

Private Sub ForceNumeric(Box As TextBox)
  On Error GoTo Catch
  Dim nStyle As Long
  
  nStyle = GetWindowLong(Box.hWnd, GWL_STYLE)
  Call SetWindowLong(Box.hWnd, GWL_STYLE, nStyle Or ES_NUMBER)
  GoTo Finally
  
Catch:
  Call MsgBox(Err.Description, vbCritical Or vbOKOnly, "Error")
Finally:
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 102 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.