How to programatically set menu shortcut keys in .NET?
In my application, I have a "recent files" drop down menu. It will contain anywhere between 0 and 9 files to load. I want to set shortcut keys on these menu items such that Ctrl+1 loads the first file, Ctrl+2 loads the second, etc...
I understand that I need to set the ShortcutKeys
property of the ToolStripMenuItem
but I开发者_运维技巧 am looking for a way to do this inside of a loop. I have the files in an array that I read them from when initially building the menu.
I'd like to be able to do something like...
for (int i = 0; i < files.Count; i++)
files[i].ShortcutKeys = Keys.Control + Keys.D0 + i;
But addition of integer types to enum types is not allowed.
Is my best solution to create some function that encapsulates a switch statement?
Something like this:
for (int i = 0; i < files.Count; i++)
if(i <= 12) files[i].ShortcutKeys = Keys.Control | (Keys)i+48;
精彩评论