01-01-2008, 11:15 AM
Hi. Im making a user control which has some labels on it. Each label might have one of those mnemonics in the caption eg "&File". How do i make an event run when i press alt+F? Do i have to do this in the owner forms key down event?
01-01-2008, 03:09 PM
Set the keypreview property to true in the usercontrol.
Use a select statement inside the UserControl's KeyUp event to capture the the keystrokes desired and run the subroutines if they are internal to the UserControl.
If the subroutines are external then you can pass the keystrokes back to the parent form by raising a UserControl 'Event' or as an alternate method pass objects into your usercontrol that can perform the required actions.
In both cases the Usercontrol will have to have the focus for the keystrokes to be captured.
If you want to capture the keystrokes even when the UserControl does not have the focus then as you suggest above you would use the Form's keyup event to do the capture and processing then perhaps wiggle some properties or methods in the usercontrol for effects.
01-01-2008, 03:12 PM
The user control cant have focus so i guess ill have to use the forms key down event with keypreview on
01-01-2008, 03:13 PM
Yes. To have keypresses handled globally you need to set the Form's KeyPreview property. Then you could catch those keys in the KeyDown event handler.
A UserControl should only expect to handle keypresses occurring when it has focus, just as a Form wouldn't expect to get another Form's keypresses.
01-01-2008, 04:08 PM
Thats ok then. Ill just have the form forward the event to the control