i want to get keypress event in windows panel control in c#, is any body help for me...
+3
A:
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
2010-01-25 12:25:27
hi thanksactually i am not getting Keypress Eventhandler for my panel control can u tell me how get that..
ratty
2010-01-25 12:36:04
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 Faciu
2010-01-25 12:48:55
A:
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
2010-01-25 12:51:23
+1
A:
"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);
}
Picsdonald
2010-03-16 10:00:55