Results 1 to 6 of 6

Thread: Numeric characters

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574

    Numeric characters

    Feeling foolish coming up with these silly things but God knows why, the last few days, I've been hamstrung by these trivial errors.

    This code does what it is supposed to do only while I step through it. When I run it, whether in the IDE or in the EXE, it does not block non-numeric characters. It let's me enter all kinds of characters.


    Code:
    Private Sub txtNewIPAddress_KeyDown(KeyCode As Integer, Shift As Integer)
    
        Select Case KeyCode
            Case vbKeyBack Or vbKeyDelete Or Asc(".") Or vbKeyHome Or vbKeyEnd
            Case 48 To 57
            Case Else
                KeyCode = 0
        End Select
        
    End Sub

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    I don't fully understand what you are trying to do.
    Do you want to assign the KeyCode a "0" whenever it is not between 48 and 57 or one the other Keys?
    In this case I would put it like that:
    VB Code:
    1. Private Sub txtNewIPAddress_KeyDown(KeyCode As Integer, Shift As Integer)
    2. If Not (KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Or KeyCode = Asc(".") Or KeyCode = vbKeyEnd Or (48 <= KeyCode <= 57)) Then
    3.     KeyCode = 0
    4. End If
    5.  
    6. End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574
    Thanks. But tell me, what difference would that make? The problem persists. Please remember that it runs while stepping through the code but not while running it.

  4. #4
    Addicted Member
    Join Date
    Dec 2002
    Location
    Malaysia
    Posts
    224
    if you want to block no numeric characters .. you can use

    isNumeric() function...
    and then verify the ascii value of it to make sure it is not any of the symbols like "/", "(", etc


    why dont you try this?

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    I made a small change, but I don't think that'S your problem. ???
    VB Code:
    1. Private Sub TextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
    2. If (KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Or KeyCode = 190 Or KeyCode = vbKeyEnd Or (48 <= KeyCode And KeyCode <= 57)) Then
    3.  
    4. Else
    5.     KeyCode = 0
    6. End If
    7.  
    8. End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I think keydown won't work since there is keyup also. So unless you put the same code in the keyup event you can use the keypress event as following. If you step through the code the keyup event won't fire if I am not mistaken :

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> vbKeyBack) And (KeyAscii <> vbKeyBack) And (KeyAscii <> vbKeyDelete) And (KeyAscii <> Asc(".")) And (KeyAscii <> vbKeyHome) And (KeyAscii <> vbKeyEnd) Then
    3.         KeyAscii = 0
    4.     End If
    5. End Sub



    Has someone helped you? Then you can Rate their helpful post.

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