altralaser
altralaser

Reputation: 2073

Using ToolStripMenuItem shortcuts in C#

I have a menu item to open a file. This item should be selected by typing the letter "o" or be activated by typing the keys Ctrl+O. So I created the following object:

fileOpenMenu = new ToolStripMenuItem();
fileOpenMenu.Name = "fileOpenMenu";
fileOpenMenu.ShortcutKeys = Keys.Control | Keys.O;
fileOpenMenu.Text = "&Open...";
fileOpenMenu.Click += new EventHandler(FileOpenMenu_Click);

If I go to the menu then there is the letter "o" displayed in the right of the open item. Is this correct? I expected that the text "Ctrl+O" is displayed on right side because this shortcut is defined. Is there a way to automatically show the shortcut text instead of the access key letter?

Upvotes: 2

Views: 5399

Answers (2)

James
James

Reputation: 2058

I have a ToolStripMenuItem with a Ctrl + O shortcut. I see "Ctrl + O" as text in the menu item. This should be the default behavior.

My code looks like this:

menuItem.ShortcutKeys = System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O;

Upvotes: 0

Try setting fileOpenMenu.ShortcutKeyDisplayString to the string value you desire to appear.

More info in this link.

Upvotes: 2

Related Questions