Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Limit Keypress Input

Limit Keypress Input

Scheduled Pinned Locked Moved Visual Basic
question
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K KaptinKrunch

    Keys.Back = the backspace key. Select case e.keychar Case Keys.Back Case Keys.D1 Case Keys.D2 End select Hope it helps..

    I Offline
    I Offline
    icowa
    wrote on last edited by
    #6

    Hmm, another something to test out... Thanz man....BTW, whats keys.d1?

    K 1 Reply Last reply
    0
    • I icowa

      Alrite man, ur method is a life Saver. Dude, I offer u my respect... U r the Man....

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #7

      *grin* Glad to help. after your initial kind comments, I was worried you'd try it and it wouldn't work.... :-) Christian Graus - Microsoft MVP - C++

      I 1 Reply Last reply
      0
      • C Christian Graus

        *grin* Glad to help. after your initial kind comments, I was worried you'd try it and it wouldn't work.... :-) Christian Graus - Microsoft MVP - C++

        I Offline
        I Offline
        icowa
        wrote on last edited by
        #8

        btw, why is there a 'not' in e.handled = 'not' char.isnumber(e.keychar) doesn't char.isnumber(e.keychar) referring to numbers in general.. if So, why does e.handled = 'not' of it..

        C 1 Reply Last reply
        0
        • I icowa

          Hmm, another something to test out... Thanz man....BTW, whats keys.d1?

          K Offline
          K Offline
          KaptinKrunch
          wrote on last edited by
          #9

          D1 = Number 1 D2 = Number 2 and soo on..... Take a look into the Keys Enumeration, I bet you find it handy! http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformskeysclasstopic.asp[^]

          I 1 Reply Last reply
          0
          • I icowa

            btw, why is there a 'not' in e.handled = 'not' char.isnumber(e.keychar) doesn't char.isnumber(e.keychar) referring to numbers in general.. if So, why does e.handled = 'not' of it..

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #10

            if it is a number, a space or a control character, you want the base class to handle the keypress (i.e. you want it to occur ). So, you set handled to false if it's a key you want, or true if you want to swallow the keypress, the base class sees you've handled the key press and so ignores it. Christian Graus - Microsoft MVP - C++

            1 Reply Last reply
            0
            • K KaptinKrunch

              D1 = Number 1 D2 = Number 2 and soo on..... Take a look into the Keys Enumeration, I bet you find it handy! http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformskeysclasstopic.asp[^]

              I Offline
              I Offline
              icowa
              wrote on last edited by
              #11

              Man, I really learn a thing or two from KaptinKrunch and Christian... Respect to you both, Dudes.... RESPECT...

              C 1 Reply Last reply
              0
              • I icowa

                Man, I really learn a thing or two from KaptinKrunch and Christian... Respect to you both, Dudes.... RESPECT...

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #12

                The Keys enumeration is more helpful if you handle KeyDown or KeyUp ( which recieves an instance of this enum, but won't let you swallow a keypress as you want ). The keypress event gets a char. Also, using the char.IsControl method covers all control keys for free, it's much more elegant code than a big old switch statement, although the switch statement gives you more control if you need it. Christian Graus - Microsoft MVP - C++

                1 Reply Last reply
                0
                • I icowa

                  Ok, lets say I create a new form. In that form I put in textbox called 'txtBox1'. Now what I wanna do is, I want to limit the characters that I can key into 'txtbox1' to numbers and the backspace key. So this is the code I use. Private Sub mskDateEntry_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles mskDateEntry.KeyPress Dim CharAllow as String = "1234567890" if instr(CharAllow,e.keychar.tostring) = 0 then e.handled = true End if End Sub Okay, this works fine in only allowing me to key in numbers and nothing else. But it also prevents me from using the backspace key to 'backspace errors'. Anyone have any idea to my situation? Thanz..

                  K Offline
                  K Offline
                  kanagaraj kumar
                  wrote on last edited by
                  #13

                  Do you have a working example of that somewhere? All I need is a simple textbox that limits its value to numeric (limiting the lenght and the number of decimals) Ie it should accept two decimals only after the decimal point ie 0.12 and not 0.123 and also the maxlentgth is 9 then 999999.99 only kanagaraj kumar

                  I 1 Reply Last reply
                  0
                  • K kanagaraj kumar

                    Do you have a working example of that somewhere? All I need is a simple textbox that limits its value to numeric (limiting the lenght and the number of decimals) Ie it should accept two decimals only after the decimal point ie 0.12 and not 0.123 and also the maxlentgth is 9 then 999999.99 only kanagaraj kumar

                    I Offline
                    I Offline
                    icowa
                    wrote on last edited by
                    #14

                    hmm. the only thing I figured out with the help of some good guys is to limit the keypress to numerical values and not the decimal values. Try opening a new thread and ask.. Sorry I ain't much of a help here, fresh beginner myself...But dun worry, the guys in here is cool and helpful, dun be shy or afraid to ask... hehehe..

                    K 1 Reply Last reply
                    0
                    • I icowa

                      hmm. the only thing I figured out with the help of some good guys is to limit the keypress to numerical values and not the decimal values. Try opening a new thread and ask.. Sorry I ain't much of a help here, fresh beginner myself...But dun worry, the guys in here is cool and helpful, dun be shy or afraid to ask... hehehe..

                      K Offline
                      K Offline
                      kanagaraj kumar
                      wrote on last edited by
                      #15

                      Public Function AcceptNumeric(ByRef TextBox1 As TextBox, ByVal decLength As Integer, ByVal eKeyChar As Char) As Boolean Dim dotIndex As Integer Dim decPlace As Integer Dim eHandled As Boolean decPlace = TextBox1.MaxLength - decLength - 1 dotIndex = TextBox1.Text.IndexOf(".") If (eKeyChar < "0" Or eKeyChar > "9") Or eKeyChar = "." Or Asc(eKeyChar) = &H8 Then If eKeyChar = "." Then If dotIndex > 0 Then eHandled = True Else eHandled = False End If Else If Asc(eKeyChar) = &H8 Then eHandled = False Else eHandled = True End If End If AcceptNumeric = eHandled Exit Function Else If TextBox1.Text.Length = decPlace Then If dotIndex < 0 And eKeyChar <> "." Then TextBox1.Text = TextBox1.Text + "." TextBox1.SelectionStart = decPlace + 1 AcceptNumeric = eHandled Exit Function End If End If If TextBox1.Text.Substring(dotIndex + 1).Length = decLength And dotIndex >= 0 Then eHandled = True End If End If AcceptNumeric = eHandled End Function *** Assign the MaxLength Property of the TextBox. kanagaraj kumar

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups