10

i want to get keypress event in windows panel control in c#, is any body help for me...

ratty
  • 13,216
  • 29
  • 75
  • 108
  • 1
    Click on the panel. 2nd tab of properties (of the panel) show events that it can fire. Double click on KeyPressEvent and voila – RvdK Jan 25 '10 at 12:51
  • 1
    @PowerRoy, that won't work, Panel does not expose the KeyPressEvent method. It only exposes the PreviewKey event. – James Jan 25 '10 at 13:49
  • PreviewKey works fine if you set the focus to the paenl in its mousedown event. – TaW Oct 19 '14 at 11:05

4 Answers4

13

You should handle the Panel.KeyPress event.

Example

public void MyKeyPressEventHandler(Object sender, KeyPressEventArgs e)
{
    ... do something when key is pressed.
}

...

(MyPanel as Control).KeyPress += new KeyPressEventHandler(MyKeyPressEventHandler);
James
  • 80,725
  • 18
  • 167
  • 237
  • hi thanks actually i am not getting Keypress Eventhandler for my panel control can u tell me how get that.. – ratty Jan 25 '10 at 12:36
  • Just try the example that James wrote. (MyPanel as Control).KeyPres will be there, if you write exactly (MyPanel as Control) where MyPanel is your windows panel control. – Adrian Fâciu Jan 25 '10 at 12:48
  • 10
    I know this is a little late in the game, but the Panel Control won't receive Key related events without focus, and clicking on the panel won't naturally give it focus. You can get around this by calling MyPanel.Focus() in a MouseDown event handler for the Panel. Once the Panel has focus, then the KeyUp, KeyDown, and KeyPress events will function, even without KeyPreview activated on the parent Form. – user1689175 May 09 '14 at 21:40
  • @user1689175 this is a fairly old answer, and in fact if you inspect the docs now they indicate that using `Panel.KeyPress` is not recommended. However, you need to look at the question being asked, there is no context it's a straight question which was given a straight answer. – James Jul 02 '14 at 10:19
9

The problem is, that at first your main form got the KeyPress and will immediately send this message to the active control. If that doesn't handle this key press it will be bubbled up to the parent control and so on.

To intercept this chain, you have to in your Form.KeyPreview to true and add an handler to Form.KeyPress. Now you can handle the pressed key within your form.

Oliver
  • 43,366
  • 8
  • 94
  • 151
3

"Panel" objects cannot receive the "KeyPress" event correctly.

I've created Panel overload:

public class PersoPanel : Panel

and used the overridden method ProcessCmdKey:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

to intercept pressed keys:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    MessageBox.Show("You press " + keyData.ToString());

    // dO operations here...

    return base.ProcessCmdKey(ref msg, keyData);
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Picsdonald
  • 31
  • 1
1

Panel + Keypress - C# Discussion Boards - CodeProject

http://www.codeproject.com/Messages/704386/Panel-plus-Keypress.aspx

ratty
  • 13,216
  • 29
  • 75
  • 108