bryanwells
asked on
key press to form only; not child controls
I have a form with child controls. When the form opens, one of the child controls (a radio button) gets focus. Then when I press the up/down arrow keys, a radio button group selection changes.
What I want is for the form to always get the key press events and I dont ever want the child controls to get a key press event. I tried over-riding the OnKeyDown method, but since a child control on the form has focus, it gets the key press event and not the parent form.
How can I cause the parent form to get all key press events and stop them from being propigated to the child controls? The user still needs to interact with the controls (sliders, radio button, etc), but even if a slider has focus, if the user presses a key, I want the parent form to get this event and cosume it and not let the slider get it.
thanks
Bryan
What I want is for the form to always get the key press events and I dont ever want the child controls to get a key press event. I tried over-riding the OnKeyDown method, but since a child control on the form has focus, it gets the key press event and not the parent form.
How can I cause the parent form to get all key press events and stop them from being propigated to the child controls? The user still needs to interact with the controls (sliders, radio button, etc), but even if a slider has focus, if the user presses a key, I want the parent form to get this event and cosume it and not let the slider get it.
thanks
Bryan
Have you tried setting the Form's KeyPreview property to True? With this event Form's KeyDown, KeyUp, and KeyPress events will be raised BEFORE those of the controls.
Sorry, forget my comment. I thought I was into the VB topic!
I just check and the KeyPreview property is existing in the .Net Framework.
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsFormClassKeyPreviewTopic.asp
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsFormClassKeyPreviewTopic.asp
Try setting Form.KeyPreview to true. Then in the keyboard event set e.Handled = true. This will stop any controls from receiving the input.
For example:
private void FormX_KeyDown(object sender, System.Windows.Forms.KeyEv entArgs e) {
if (e.Alt && e.KeyCode == Keys.F4 || e.KeyCode == Keys.Escape) {
// Do some processing if the Escape key or Alt+F4 is pressed. i.e. FormX.Close()
e.Handled = true;
}
}
For example:
private void FormX_KeyDown(object sender, System.Windows.Forms.KeyEv
if (e.Alt && e.KeyCode == Keys.F4 || e.KeyCode == Keys.Escape) {
// Do some processing if the Escape key or Alt+F4 is pressed. i.e. FormX.Close()
e.Handled = true;
}
}
ASKER
This almost works. But my radio buttons come up selected first, and the arrow keys still toggle the radio buttons without getting a call to my key down event handler in my form. Once I put focus on another control, it works as expected. Is there something special about radio buttons and up/down arrow keys?
Radio buttons always act as a group in a container. The container in this case is the form.
Try putting 'this.ActiveControl = MyControl' in your FormX_Load event. This should put the focus on another control (MyControl) before the form is visible.
Try putting 'this.ActiveControl = MyControl' in your FormX_Load event. This should put the focus on another control (MyControl) before the form is visible.
ASKER
This is very close to what I want. I just wish I could stop the arrow keys from toggling the radio buttons. I have them in a group box instead of in the form directly, but still it is the same behavior as you described. How can I disable radio buttons from responding to the up/down arrow keys?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.