Accelerators

  • Thread starter Thread starter Matthew Ireland
  • Start date Start date
M

Matthew Ireland

How do you create Accelerator behavior for a Windows Form exe with a menu
(i.e. <ALT> + O to call an Open File method). The property on the Menu Item
doesn't seem to do anything except display some text indicating an
accelerator is present. There is no way to connect the key sequence to a
method that I can find.
 
Matthew Ireland said:
How do you create Accelerator behavior for a Windows Form exe with a menu
(i.e. <ALT> + O to call an Open File method). The property on the Menu
Item doesn't seem to do anything except display some text indicating an
accelerator is present. There is no way to connect the key sequence to a
method that I can find.

This is done internally by Windows. However, you can simply add a MainMenu
component to the form, add the menu items, assign shortcuts, and place the
code which should be executed when pressing the shortcut in the menu items'
'Click' event handlers. Note that the menu doesn't even need to be visible.

An alternate approach is to set the form's 'KeyPreview' property to 'True'
and add handling code in the form's 'KeyPress'/'KeyDown'/'KeyUp' event
handlers.
 
The accelerator calls the menu item's event handler. You need to wire up an
event handler for the menu item's Click event.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
This does not happen. The shortcut I want to use is one level down from the
main menu (File/Load). I have set property for the Load submenu item
"ShortcutKeys" to a value (<ALT> + D), set the property "ShowShortcutKeys"
to true and created a 'Click' event handler. When I press <ALT> + D, the
"File" item on the main menu gets highlighted, but the submenu item does not
get invoked. If I press <ALT> + F, the "File" menu gets opened and I can
then press "D" to get the load behavior, but I don't want to have to go
through the extra keystroke. I want an accelerator, just like in regular
windows Apps.
 
This does not happen. The shortcut I want to use is one level down from the
main menu (File/Load). I have set property for the Load submenu item
"ShortcutKeys" to a value (<ALT> + D), set the property "ShowShortcutKeys"
to true and created a 'Click' event handler. When I press <ALT> + D, the
"File" item on the main menu gets highlighted, but the submenu item does not
get invoked. If I press <ALT> + F, the "File" menu gets opened and I can
then press "D" to get the load behavior, but I don't want to have to go
through the extra keystroke. I want an accelerator, just like in regular
windows Apps.
 
Matthew Ireland said:
This does not happen. The shortcut I want to use is one level down from
the main menu (File/Load). I have set property for the Load submenu item
"ShortcutKeys" to a value (<ALT> + D), set the property "ShowShortcutKeys"
to true and created a 'Click' event handler. When I press <ALT> + D, the
"File" item on the main menu gets highlighted, but the submenu item does
not get invoked. If I press <ALT> + F, the "File" menu gets opened and I
can then press "D" to get the load behavior, but I don't want to have to
go through the extra keystroke. I want an accelerator, just like in
regular windows Apps.

Are you using mainmenu or menustrip?
 
I think the ALT key is getting handled by the Form. This key activates the
first menu item by default. I use the CTRL key in my menus, and have no such
issues. You might go with that. Otherwise, you may have to intercept the ALT
key in the Forms message loop, which would be difficult.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

Matthew Ireland said:
I am using MenuStrip. It was the only Menu available in the toolbox.
 
Back
Top