enabled numeric key but disabled other key


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: enabled numeric key but disabled other key

  1. #1
    Join Date
    Aug 2004
    Posts
    25

    enabled numeric key but disabled other key

    If KeyAscii < 48 Or KeyAscii > 57 Then
    KeyAscii = 0
    ElseIf KeyAscii = 13 Then
    If QtyArvd <> "" Then
    Amt.SetFocus
    ElseIf QtyArvd = "" Then
    QtyArvd.SetFocus
    End If
    End If

    i created the above code to enable the accept number key only but why is that it doesnt accept backspace and it doesnt have any keypress, i mean the enter key is also disable even if i specify it in "elseif keyascii=13"

  2. #2
    Join Date
    Apr 2005
    Location
    South Africa
    Posts
    88
    Does this help?
    Code:
    Private Sub Text1_KeyPress(KeyCode As Integer)
        If ((KeyCode < 48) Or (KeyCode > 57)) Then
           If ((KeyCode <> 13) And (KeyCode <> 8)) Then
               KeyCode = 0
           End If
        End If
        If KeyCode = 13 Then
           'Change field
           Text1.SetFocus
        End If
    End Sub
    GermanD
    deonmeyer@gmail.com
    ** <<]db[>> **
    *** G.s.G Crew ***

  3. #3
    Join Date
    Aug 2004
    Posts
    25
    yes sir it helps
    here is the final one


    If ((KeyAscii < 48) Or (KeyAscii > 57)) Then
    If ((KeyAscii <> 13) And (KeyAscii <> 8)) Then
    KeyAscii = 0
    End If
    End If
    If KeyAscii = 13 Then
    If QtyArvd <> "" Then
    Amt.SetFocus
    ElseIf QtyArvd = "" Then
    QtyArvd.SetFocus
    End If
    End If

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    This is one of the cases where select case gives a cleaner solution. I changed also the ascii with the KeyCodeConstants (a little easierr to read)

    Code:
    select case KeyAscii
      case vbKeyReturn
        ''... special handling for Return
      case vbKeyUp, vbKey0 to vbKey9 ''<- add here all the characters that are OK
        ''.. OK
      case else
        KyAscii = 0
    end select
    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center