开发者

How to programatically set menu shortcut keys in .NET?

开发者 https://www.devze.com 2023-01-04 04:44 出处:网络
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 fi

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消