Setting the Windows Forms ToolStripMenuItem ShortcutKeys property to numpad key does not work

有些话、适合烂在心里 提交于 2019-12-19 09:01:15

问题


We have the ability to define shortcut keys for Windows Forms application menu items. That way I can tell a menu item File->Save to have the shortcut key Ctrl + S and the menu item's handler is "magically" executed after pressing Ctrl + S.

The trouble is with the numeric keypad keys, the ShortcutKey property does not accept them (I don't understand how are they different from the other acceptable keys).

MSDN states that the property accepts type System.Windows.Forms.Keys (One of the Keys values. The default is None.); and an InvalidEnumArgumentException would be thrown when the parameter is not one of Keys values. But for example Keys.Divide IS one of Keys values, and yet it can't be used.

So how can I set a menu item to have a shortcut for Numpad * or Numpad +? Do I need to handle the key in Form's ProcessCmdKey event?


回答1:


You must use Ctrl or Alt in shortcuts.

example:

//working:  
toolStripMenuItem1.ShortcutKeys = Keys.Control | Keys.NumPad0;  
//throws exception  
toolStripMenuItem1.ShortcutKeys = Keys.NumPad0;  



回答2:


To answer the question about using Numpad * and Numpad +:

  • Numpad * is called the multiply key. The enumeration name is Multiply.

  • Numpad + is called the add key. The enumeration name is Add.

  • Numpad - is called the subtract key. The enumeration name is Subtract.

It is not possible to select these in Visual Studio's property window (at least not in Visual Studio 2008), but the corresponding source code can be edited; where property ShortcutKeys is set. For example for Numpad + for a menu item named mnuMoreTime:

this.mnuMoreTime.ShortcutKeys = 
    ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control |
                                  System.Windows.Forms.Keys.Add)));


来源:https://stackoverflow.com/questions/2708909/setting-the-windows-forms-toolstripmenuitem-shortcutkeys-property-to-numpad-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!