CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 1999
    Location
    Scotland
    Posts
    7

    Textbox for only numerical input including -ve values

    I am trying to filter the input to a textbox such that:

    1. Only numerical values will be accepted.
    2. A decimal point will only be allowed once.
    3. Negative values will be allowed but only with
    the -ve sign at the beginning of the input and
    only being accepted once.

    I have tried the DataFormat property and this does not seem to work. I seem to be able to trap the text values and decimal point constraints but the -ve has escaped me so far...

    Any suggestions???

    I am quite new to VB and may have missed something fundamental. Perhaps there is an API Function angle to this one with which I am not familiar...

    Thanks


  2. #2
    Join Date
    Jan 2000
    Posts
    21

    Re: Textbox for only numerical input including -ve values

    Here is most of what your looking for; you should
    be able to figure the rest out, i.e. -ve, only once etc.,(vbsubract is the - key, which is ascii 109,I think)just set up a trap or counter for it

    public Sub NumbersOnly(KeyAscii as Integer)
    '***********************************************************
    '* Subroutine: NumbersOnly
    '* Description: Permits only numbers,
    '* Delete Key, Backspace Key and Enter Key to be input
    '* to the control calling this function
    '***********************************************************

    Select Case KeyAscii
    Case vbKeyDelete 'allow Delete Key
    Case vbKeyBack 'allow Backspace Key
    Case vbKeyReturn 'allow Enter Key
    KeyAscii = 0
    SendKeys "{TAB}"
    Case 48 to 57 'allow Numeric Keys
    Case else
    Beep
    KeyAscii = 0
    End Select
    End Sub

    'to call the routine
    NumbersOnly KeyAscii




    another method using Instring:


    'Place a Text Box on a Form add the following to
    'it's KeyPress Event

    private Sub Text1_KeyPress(KeyAscii as Integer)
    If InStr("0123456789-.", Chr(KeyAscii)) = 0 And _
    KeyAscii <> vbKeyBack then KeyAscii = 0
    End Sub







  3. #3
    Guest

    Re: Textbox for only numerical input including -ve values

    I am not sure how to do it to a inputbox but i do to a textbox:
    If Not IsNumeric(text1.Text) Then
    MsgBox "Numbers Only!!!"
    End If




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