how to trap this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pakaulaw
    New Member
    • Sep 2008
    • 3

    how to trap this?

    in a textbox, users should input numbers and if letters are inputted, it will create an error that only numbers are accepted. please help me. i am using vb6 and windows xp. thanks.
    Last edited by pakaulaw; Sep 30 '08, 06:14 AM. Reason: update
  • jg007
    Contributor
    • Mar 2008
    • 283

    #2
    you could catch the ascii code of the entered character and then remove the last character entered if it is not within the right values.

    I have got some VB .net code to do this that I have worked out but I would rather you tried to do this yourselft and post any code that you are having problems with as it does not help you if I post the full code

    if it is numeric you might also be able to use ' isnumeric ' rather than actually checking the ascii code
    Last edited by jg007; Sep 30 '08, 04:23 PM. Reason: reread post :)

    Comment

    • Montravont
      New Member
      • Sep 2008
      • 10

      #3
      There is actually a code that you can place in the keypress event for the text box that will limit it to only accepting numbers, delete and backspace. As jg007 said... attempt to work it out and let us know where you get and we can help you more from there. Or show us what you already have that you're trying to use to do so.

      Comment

      • pakaulaw
        New Member
        • Sep 2008
        • 3

        #4
        its ok guys.. i used the vbkey constants so that any values entered other than the supposed to be values only would be "cleared" right away.

        Comment

        • jg007
          Contributor
          • Mar 2008
          • 283

          #5
          please can you post your code on here as I don't think I have used that before and it would be interesting to see.

          Comment

          • pakaulaw
            New Member
            • Sep 2008
            • 3

            #6
            Private Sub BurnText1_KeyUp (KeyCode As Integer, Shift As Integer)

            If (KeyCode >= 48 And KeyCode <= 57) Or (KeyCode >= 96 _
            And KeyCode <= 105) Or (KeyCode = 8) Or (KeyCode = 32) Or (KeyCode = 191) _
            Or (KeyCode = 110) Then
            Else
            BurnText1.Text = ""
            End If
            End Sub

            that's the code i've used and it works like a charm.. now, if you want to trap for letters only to be inputted, you should change the constant values. not all constant values are readily available in vb6 so you have to research for a list of keycode constant values,.

            hope this helps.

            Comment

            • jg007
              Contributor
              • Mar 2008
              • 283

              #7
              your code is great and will work but as a reminder that there are always plenty of way to do something :) , this could be easily amended to allow letters only although would require extra code to avoid special character

              this code also avoids clearing the textbox when invalid characters are entered -

              that is the fun thing of programming trying to work out how to achieve a goal and although I just use whatever works for me several months down the line I inevitably find another way to do it

              Code:
               Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
              
                      If Not IsNumeric(e.KeyChar) Then
                          e.KeyChar = Nothing
                      End If
              
                  End Sub

              Comment

              Working...