Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pause the keyboard input? 1

Status
Not open for further replies.

MrDataGuy

Programmer
Oct 18, 2010
231
0
0
US
I have a textbox that number are entered into. I am allowing the user to type a ‘-‘ to reduce the number by one, and ‘+’ to increase it by one. (In the Keyup method there is code looking for e.KeyCode = Keys.Subtract and = Keys.Add). The code does more then adding or subtracting but that is a side issues. What I am finding is that sometimes the user presses down on the ‘+’ or ‘-‘ keys to do rapid changes and the code crashes because it is trying to process ‘+++++++’ or ‘-------‘ etc.

I can try and handle this situation in several ways, but I am wondering if there is a command that pauses the keyboard processing, or puts the text in a buffer so the code can be handled in a reasonable way.

Any ideas? Suggestions?


Lion Crest Software Services
Anthony L. Testi
President
 
Sounds like you would like to intercept and process the plus and minus keys before they get to the textbox.
Code:
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If (e.KeyChar = "-"c OrElse e.KeyChar = "+"c) Then
            e.Handled = True '<- This is what keeps it being sent to the textbox
            ' Add or subtract to the textbox value here
        End If
    End Sub
 
Dave
Short response: THANK YOU; you gave me the perfect answer, take a bow and give yourself a high five.

Long response:
I started this code sometime ago and copied an example of what I needed (Trapping and dealing with keystrokes, not the specific incrementing and decrementing stuff) and have modified it since. I looked at your example code and saw the e.Handled command. I said to myself well that is of no help the code already does that. Then I did a 2nd look and then thought that maybe the e.Handled was needed in the Keyup method, when I had it in the KeyDown method. Looking at your example a 3rd time I realized that you were using the (unknown to me) Keypress method. Hmmm it is starting to get interesting. I looked at the keypress method and modified my code to use that instead of the KeyUp and Keydown methods and it all works like a charm. So I learned something today! Thanks again!


Lion Crest Software Services
Anthony L. Testi
President
 

Just curious,

What does this 'c' do next to "-" or "+" sign in the code?


Have fun.

---- Andy
 
That means that the previous string should be interpreted as a Char type rather than a string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top