CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Guest

    Using textbox for numeric data

    I am using a Textbox for data input. I would like the textbox to accept only numeric data, is there some parameter/attribute that I can set for this?


  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Using textbox for numeric data

    Set the Tag Property of any Textbox you want to be Numeric Only to N and use this:

    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 GWL_STYLE = (-16)
    private Const ES_NUMBER = &H2000

    private Sub Form_Load()
    Dim oCTRL as Control
    for Each oCTRL In me
    If TypeOf oCTRL is TextBox then
    If UCase(oCTRL.Tag) = "N" then
    'Make the Textbox Numeric Only
    Call SetWindowLong(oCTRL.hwnd, GWL_STYLE, _
    GetWindowLong(oCTRL.hwnd, GWL_STYLE) _
    Or ES_NUMBER)
    End If
    End If
    next
    End Sub




    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  3. #3
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: Using textbox for numeric data

    Or you can use this much simpler code
    1) Make a textbox (text1) and set it's TEXT property to "" (nothing) so you can see the result

    private Sub Text1_Change()
    If Not IsNumeric(Text1) then MsgBox "Not a number"
    End Sub



    Good luck!


  4. #4
    Join Date
    Jan 2000
    Posts
    72

    Re: Using textbox for numeric data

    Private Sub Text2_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case 48 To 57:
    ' Allow digits
    'enter code for what should
    'be done if numeric

    Case Else
    KeyAscii = 0
    Beep
    End Select
    End Sub

    refer ascii chart for the equivalent codes



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured