Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I work around menu shortcuts showing up incorrectly when the user uses Ctrl+Number?

When you assign Ctrl1, Ctrl2, etc. as shortcuts for MenuItems they show up as Ctrl+D1, Ctrl+D2. This can be worked around by creating and adding the menuitem through code as demonstrated below:

[C#]

//Create the menuitem
MenuItem mymenuItem = new MenuItem();
//ShortCut
mymenuItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl1;
//Add Event Handler for the menuitem
mymenuItem.Click +=new EventHandler(this.mymenuItem_Click);
//ShortCut Text to be displayed
mymenuItem.Text = "My MenuItem" +"\t"+ "Ctrl+1";
//hide shortcut
mymenuItem.ShowShortcut = false;
//Add it to the bottom of the first menu
this.mainMenu1.MenuItems[0].MenuItems.Add(mymenuItem);

[VB.NET]

'Create the menuitem
Dim mymenuItem As MenuItem = New MenuItem()
'ShortCut
mymenuItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl1
'Add Event Handler for the menuitem
mymenuItem.Click +=New EventHandler(Me.mymenuItem_Click)
'ShortCut Text to be displayed
mymenuItem.Text = "My MenuItem" +"\t"+ "Ctrl+1"
'hide shortcut
mymenuItem.ShowShortcut = False
'Add it to the bottom of the first menu
Me.mainMenu1.MenuItems(0).MenuItems.Add(mymenuItem)

Contributed from George Shepherd's Windows Forms FAQ