[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1716
  • Last Modified:

Detect Arrow Keys in KeyPress Event

In a TextBox1_KeyPress event i want to detect of the user presses an arrow key (up, down, left, right). Would i use something like....

If e.KeyChar = ????? Then
msgbox ("Success!")
End if

If so, what would the value of the KeyChar be fore each of the arrow keys??  thanks!
0
NewAgeNemesis
Asked:
NewAgeNemesis
  • 3
  • 2
  • 2
  • +2
1 Solution
 
Bob LearnedCommented:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    If e.KeyCode = Keys.Right Then
      MessageBox.Show("Right")
      e.Handled = True
    End If

  End Sub

Bob
0
 
bman9111Commented:
thelearnedone sorry to impose but what is the difference between keydown and keypress

  Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    End Sub

  Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    End Sub
0
 
bman9111Commented:
u don't have to answer, I was just curious
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Fernando SotoCommented:
The KeyPress event will not be triggered when pressing the arrow keys. The KeyPress event only handles printable characters and the return key. What you may do is use the KeyDown event.

Imports System.Windows.Forms

Private Sub tbName_KeyDown(ByVal sender As Object, ByVal e As _  System.Windows.Forms.KeyEventArgs) Handles tbName.KeyDown
Dim Key As Keys

Select Case e.KeyCode()
    Case Keys.Left
        Console.WriteLine("Left Arrow Key Pressed")
    Case Keys.Right
        Console.WriteLine("Right Arrow Key Pressed")
    Case Keys.Up
        Console.WriteLine("Up Arrow Key Pressed")
    Case Key.Down
        Console.WriteLine("Down Arrow Key Pressed")
End Select

End Sub
0
 
Bob LearnedCommented:
Certain control keys, such as the arrow keys are not trapped by the Keypress event handler, but they are trapped by the KeyDown event handler.

From the help:
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

Bob
0
 
Fernando SotoCommented:
I was not trying to hijack YOUR QUESTION. I was attempting to give  NewAgeNemesis a full and complete answer to his question. If you look at the time stamp between my answer and the question from  bman9111 you will see it was 2 minutes the time it took me to andwer the question. But please except my apologies if I have offended you in any way.
0
 
bman9111Commented:
thelearnedone, not sure if u are refering to me, if so sorry, like I stated before

thelearnedone sorry to impose but what is the difference between keydown and keypress


could of just stated that I needed to open a question, I have unlimited points, from answer other questions too. I just seem this and had to ask...



really not understanding

It is very hard for me to control myself when other people try to hijack my questions, thinking that I can't handle the
response

Sorry for all the anger....
0
 
doobdaveCommented:
TheLearnedOne,

I didn't realize there was a rule on this site that states that the first person to post a comment to a question OWNS it.

I was under the impresssion that this is a 'collaborative' site, where several 'experts' may provide input to a given question/problem...
0
 
NewAgeNemesisAuthor Commented:
thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now