Assigning menu-shortcuts at runtime

  • Thread starter Thread starter Per Rollvang
  • Start date Start date
P

Per Rollvang

Hi All,

I got a recent-files-list on a WinForm, that gets added from a string-array
(string[]) at Form_Load, and I have difficulties assigning shortcuts to
them. I want the shortcuts to be Ctrl0,Ctrl1,...etc, based on how many items
I have in my list (max 10 items).

Is there any elegant way to do this, because I can not find out how to cast
a string to a Shortcut...

TIA,

Per Rollvang
 
menuItem2.Shortcut = (Shortcut)Enum.Parse(typeof(Shortcut), "ShiftIns");
 
Per Rollvang said:
[MRU list with shortcuts]

Is there any elegant way to do this, because I can not find out how to
cast a string to a Shortcut...

I don't understand why you want to deal with strings. You can use
'Shortcut' directly in your code.
 
Herfried K. Wagner said:
I don't understand why you want to deal with strings. You can use
'Shortcut' directly in your code.

I expect the op wanted a simple routine to assign a shortcut to each
menuitem in the Dynamically created MRU menu, which may contain upto 10
items.

One Method is to use a String and another, which I prefer, is to use an
Integer.

\\\
for (int id = 0; id < MRU.MenuItems.Count;id++)
MRU.MenuItems[id].Shortcut =
(Shortcut)Enum.Parse(typeof(Shortcut), "Ctrl" + id);
///

\\\
for (int id = 0; id < MRU.MenuItems.Count;id++)
MRU.MenuItems[id].Shortcut = Shortcut.Ctrl0 + id;
///

I expect the next question will be "Why does the shortcut 'Ctrl0' display as
'Ctrl+D0' ?".
I don't know the answer to that one.
 
Hi Mick,

"Mick Doherty"
for (int id = 0; id < MRU.MenuItems.Count;id++)
MRU.MenuItems[id].Shortcut = Shortcut.Ctrl0 + id;
///

I expect the next question will be "Why does the shortcut 'Ctrl0' display
as 'Ctrl+D0' ?".
I don't know the answer to that one.

Same on my German system ("Strg+D0").
 
I expect the next question will be "Why does the shortcut 'Ctrl0' display
It don't! It works like a charm!

Thanks a lot Mick

Regards,
Per Rollvang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top