VB6.0 : Set textbox to Numeric

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wassup
    New Member
    • May 2007
    • 34

    VB6.0 : Set textbox to Numeric

    Good morning (in Malaysia),

    I have a source code of setting the textbox for key in Numeric only. But my problem is why i can't backspace? Can anyone help me please, thanks.

    Code:
    Private Sub txtProDate_KeyPress(KeyAscii As Integer)
       If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
          KeyAscii = 0
          Beep            ' Sound error signal.
       End If
    End
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    #2
    Originally posted by wassup
    Good morning (in Malaysia),

    I have a source code of setting the textbox for key in Numeric only. But my problem is why i can't backspace? Can anyone help me please, thanks.

    Code:
    Private Sub txtProDate_KeyPress(KeyAscii As Integer)
       If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
          KeyAscii = 0
          Beep            ' Sound error signal.
       End If
    End
    The ascii code for backspace is 8, which in your code gets trapped.
    Refer the links below

    Comment

    • wassup
      New Member
      • May 2007
      • 34

      #3
      Originally posted by cmrhema
      The ascii code for backspace is 8, which in your code gets trapped.
      Refer the links below
      http://www.thescripts. com/forum/thread620015-keyascii.html
      Thanks cmrhema.
      Here the code after revise set the textbox to Numeric and allow for Backspace.

      Code:
      Private Sub txtProDate_KeyPress(KeyAscii As Integer)
         If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
            KeyAscii = 8
            Beep            ' Sound error signal.
         End If
      End Sub

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        I think you might see some quite interesting and unexpected results from that code. This version might work slightly better...
        [CODE=vb]Private Sub txtProDate_KeyP ress(KeyAscii As Integer)
        Select Case KeyAscii
        Case Is < 32
        ' Ignore
        Case Asc("0") To Asc("9")
        ' Ignore
        Case Else
        KeyAscii = 0
        Beep ' Sound error signal.
        End Select
        End Sub[/CODE]

        Comment

        • wassup
          New Member
          • May 2007
          • 34

          #5
          Originally posted by Killer42
          [CODE=vb]Private Sub txtProDate_KeyP ress(KeyAscii As Integer)
          Select Case KeyAscii
          Case Is < 32
          ' Ignore
          Case Asc("0") To Asc("9")
          ' Ignore
          Case Else
          KeyAscii = 0
          Beep ' Sound error signal.
          End Select
          End Sub[/CODE]
          Hi killer,
          I still not very understand what the diffrent of this two source code. Why this more better? Can you teach me more detail? Thanks.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Well if you follow the original code step by step, it effectively says "if the user pressed anything other than a number, then change it to a backspace". This is not very useful.

            I tried it out. What happens (as expected) is that you can type numbers, but every time you press a lett, or anything else, it just erases the last digit.

            The modified version I posted says something like this...
            • If user pressed some sort of control key (arrows, enter, whatever) then leave it alone.
            • If user pressed a numeric digit, then leave it alone.
            • If user pressed anything else, don't allow it to go through (suppress it).

            Comment

            Working...