Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a window form, and it includes several control with 'Panel' type,
now I add KeyDown event to this form and control.

I found that the keydown of form can provide left/right/up/down key detect,
however, the keydown event of control (Panel) will not provide left/right/up/down
key detect, is that right ? If yes, how can I get the left/right/up/down
key code for control keydown event ? If not, what's wrong it could be ?

Thanks of your fully support on this issue.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Mar-12 17:39pm    
Not true. This event is the event of System.Windows.Forms.Control, and Form inherits it.
--SA
Sports Kuo 28-Mar-12 7:58am    
Hi SA,
Thanks of your comments, please my description to Bjorn.

1 solution

Hi,

I think you're searching for PreviewKeyDown-Event for your Control (your panel).

C#
private void panel1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                //do something
            }
        }


With Best Regards
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Mar-12 17:40pm    
Probably. My 5.
--SA
El_Codero 27-Mar-12 18:41pm    
Still probably, answer isn't accepted yet ;)
Sports Kuo 28-Mar-12 8:09am    
Hi Bjorn,

Thanks of your help.

Please see the result of my advanced verification according to your code line on this issue.

1. Generally, a control can only have PreviewKeyDown event.
2. If create an user control with 'Panel' type, it then can add KeyDown event. (I learn this from SA before)
3. For control, it always can not get left/right/up/down key for PreviewKeyDown event or KeyDown event, since the left/right/up/down key is used by Form to select focus for child controls.
4. A Form can have KeyDown event, and this event can get left/right/up/down key

If any incorrect, please correct it, thanks very much.
El_Codero 28-Mar-12 9:34am    
Hi Sports Kuo,

1. Generally,yes.
2. Don't know what you've exactly learned from SA before, but I'm sure it's right, I think he explained you a workaround to get KeyDown event in Panel.
Code explanation:

this.ActiveControl = your_panel;

//then handling the KeyDown event
private void your_panel_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
if ((e.KeyCode == Windows.Forms.Keys.Escape)) {
MessageBox.Show("Esc-Key is Pressed);
}
}

3. Right, the reason why there's no KeyDown event is because panel doesn't get focus and can't receive Windows Messages from Keyboard. Some Key presses are ignored by some controls (i.e. panel) because they are not considered input key presses.
4. Right, because the arrow keys typically causes the focus to move to the previous/next control.

Does it make sense to you?
With Best Regards
Sports Kuo 10-Apr-12 22:31pm    
Hi Bjorn,
Sorry to late respond.
And thanks so much of your advnced and complete answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900