'cannot trap Up or Down keys...

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
I have a small message form set up with 5 or so labels and a cancel button. I want the labels to change background color as the user presses the up or down keys. It seems that I can trap all key events except the up and down keys. I've tried many combinations of:
-----------------------------------
VB.NET:
Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs) 
          If e.KeyChar = ChrW(38) Then 
               e.Handled = True
               '<...process color here ...>
          Else 
               MyBase.OnKeyPress(e) 
          End I
-----------------------------------

VB.NET:
 Private Sub Form1_KeyPress (ByVal sender As Object, ByVal e As _
 system.Windows.Forms.KeyPressEventArgs) _ Handles MyBase.KeyPress
           If e.KeyChar = ..... an so on ...
-----------------------------------
The KeyPreview for the form is set to TRUE. I've tried versions using the
VB.NET:
 "AddHandler KeyDown, AddressOf ..."
method. Nothing seems to be able to trap the arrow keys. I prefer using labels rather than buttons on this form because it is used mostly as messaging window and only needs to be able to highlight selected labels for one or two short applications.
I'm using Visual Studio.NET 2003...... 'Any ideas what I'm doing wrong ?
 
use the form's KeyDown event for this:
VB.NET:
Private Sub Form1_KeyDown (...) Handles MyBase.KeyDown
  Select Case e.KeyCode
    Case Keys.Up
       'your code here
    Case Keys.Down
       'your code here
  End Select
End Sub

i used a select case here to make it easy to add code for other keys that you want to use
 
I've tried the KeyDown routine also, still without any trapping. The HELP section in vb.NET leaves me scratching my head. Here's the only thing I can find that seems to be associated with my problem, and I don't understand exactly what it is they want...

Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. In order to have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true.

I guess my question now is, how do I override the IsInputKey method for each control ?
 
Last edited:
You're handling events of the form istead of the individual controls. If you want the form to raise those events then you have to set its KeyPreview property to True, although I'd say that you should actually handle the KeyDown events of the individual controls instead.
 
The KeyPreview property is set to TRUE. This is how I can trap all other keydown or keypress events EXCEPT for the navigation arrows. I can set a breakpoint at the trapping sub and the program breaks for all keys except the arrow keys. Everything I've read leads me to believe that this should work and yet it does not. This version of Visual Studio 2003 was a freebie from Microsoft for taking the time to preview some of their instructive movies and I'm beginning to wonder if this version is a lemon....
 
I just tested some code and I can see the KeyDown and KeyUp events for the arrow keys. Confirm that the KeyPreview property is set to True and try this code in your form:
VB.NET:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Debug.WriteLine("KeyDown: " & e.KeyCode.ToString())
    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        Debug.WriteLine("KeyPress: " & e.KeyChar.ToString())
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        Debug.WriteLine("KeyUp: " & e.KeyCode.ToString())
    End Sub
If you then keep an eye on your Output window it will show you wich events are being raised for which keys. I can't see any reason why which version of VS you are using should make a difference, but you could always download the 2005 Express editions if you don't specifically need any features that the full VS.NET 2003 provides and the Express editions do not. You'll be using the most up-to-date version of language and the Framework that way too.
 
OK... This is very strange indeed...
I used your code for the KeyUP event and it successfully trapped both the Up and Down arrow keys. I modified it slightly here.

VB.NET:
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As_ system.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyCode = Keys.Up Then
            Label1.BackColor = Color.Red
            Label2.BackColor = Color.Black
        ElseIf e.KeyCode = Keys.Down Then
            Label1.BackColor = Color.Black
            Label2.BackColor = Color.Red
        End If
        Label3.Text = e.KeyCode.ToString
    End Sub
The two other snippets you provided did not trap the arrow keys at all. I don't mind using the KeyUp event to trap this event, but it makes me wonder about the version of VS that I'm using.
Thanks a lot for your help !!!!
 
that's really weird, in vb2003 my example captures all keypresses in the keydown event with and without the keypreview being set to true
 
Well, well, well....Microsoft offered a free version of Visual Studio to those that would preview, comment on and rate their instructional VB snippets on line. What they're not telling you is that this version is less than complete. My guess is that they ran off a bunch of these CD's and, since they certainly couldn't sell them, they offered them up on line in exchange for your time and critique. I'd be curious to hear what Microsoft has to say about this. I should have known it was too good to be true. I've wasted a minimum of two days beating myself up over this issue. I was sure that I screwed up somehow. Now my thoughts are "What else is missing in this version ?"
I appreciate your bearing with me through this.
 
The thing I don't understand is that it shouldn't matter what version of VS you are using. Your IDE may or may not have certain features but that doesn't matter once the app is built. You can write an app in Notepad and build it at the commandline if you want. It is the .NET Framework itself that provides the functionality of the app, so the same version of the Framework should produce the same behaviour no matter what was used to create the app in the first place. Do you have SP1 installed for v1.1 of the Framework? I just wonder whether that's something that changed at that point. You're definitely using VS.NET 2003 and not 2002, right?
 
i know i'm using vs2003 with the framework 1.1 sp1 installed
and this works without keypreview being True:
VB.NET:
Private Sub Form1_KeyDown (...) Handles MyBase.KeyDown
  Select Case e.KeyCode
    Case Keys.Up
       'your code here
    Case Keys.Down
       'your code here
  End Select
End Sub

so he's either not using vs2003 or vs2005, or something
 
I may have spoken too soon. It's easy to blame Microsoft for anything and everything, and I apologize for that. The comment about the framework handling the events makes sense & I can't imagine that VS selectively leaves out any refs to keydown events just for the arrow keys. I am using Visual Studio 2003 but I have not installed sp1. I will do that and post back. I will be a happy camper if that solves the problem...
 
Last edited:
...and I did just that !

I downloaded SP1 from Microsoft and installed it over a previously installed SP1. (I didn't know SP1 was already on this machine)... and that solved most of the problem. The system NOW responds to all arrow keys with "KeyDown" but still does not respond to arrow keys with the "KeyPress" method.

Ya know... that's good enough for me.
Thank You very much !
 
It shouldn't respond to KeyPress events for arrows. KeyPress is only for characters that get displayed in the control. For instance, Shift+a will give you a single KeyPress event for A.
 
Back
Top