CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2000
    Location
    Pennsylvania, United States
    Posts
    106

    KeyPress Numeric enforcing

    I am using a textbox and using the keypress event
    to make the characters uppercase.

    I have another textbox that I am using that I want
    to ignore all characters except numerics. What
    value should the KeyAscii variable be set to.

    In addition I want to have the escape sequence characters not be affected. A tab should go to the next control in the tab order.

    Any thoughts??




  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: KeyPress Numeric enforcing

    Private Sub Text1_KeyPress(KeyAscii As Integer)

    Const Numbers$ = "0123456789."

    If InStr(Numbers, Chr(KeyAscii)) = 0 Then
    KeyAscii = 0
    Exit Sub
    End If
    End Sub

    Tab will send you to the next control


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: KeyPress Numeric enforcing

    It's always faster to manipulate numbers than strings. So, a better method will be

    private Sub Text1_KeyPress(KeyAscii as Integer)
    If KeyAscii >= 48 and KeyAscii <= 57 then KeyAscii = 0
    End Sub




    48 is the code for "0" and 57 for "9"


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