

We will demonstrate this using our example by wiring up the Exit menu option so that it closes the application, and by writing an event procedure for the Save on Exit menu option to change the checked status of the Checked menu item. This is achieved using the event procedures of the individual menu options.

Now that we have designed a menu system in Visual Studio, the next step is to make the menu items do something when they are selected by a user.

When it appears, click on the File and Edit menus to confirm that they appear as you intended. Press the F5 key to build and run the application. In the property panel for the checked menu item, change the Name property to SaveOnExitMenu. The menu item will now be displayed with a check mark next to it. To create a Checked item in the menu enter Save on Exit in the Type Here field and then right click on the item to display the popup menu. When a Checked item is set, a check mark appears and when they are unset the check mark disappears. A checked menu item maintains state of being either set or unset. The last task in creating our top-level menu is to add a Checked menu item. Once completed the form layout should appear as follows: Use the same technique outlined above to add Cut, Copy and Paste items to the Edit menu. Click in the Type Here field next to the File menu and enter &Edit to create an Edit menu. We will now create a second drop down menu. To do so, simply click on the separator option in the menu.Īs the final entry in the File menu, add an E&xit item. As with the ComboBox, you should resist the temptation to add such a control to a menu as it violates GUI design convention.įor our example we will add a separator to our File menu. TextBox - Adds a TextBox control to the menu.Separators in menus are useful for distinguishing between groups of menu items. Separator - Places a separator after the last menu item to be added.The placing of a ComboBox in a menu is not considered to be good user interface design. Although you have the option of adding a ComboBox to your menu, you should resist the urge to do so. ComboBox - Adds a ComboBox control to the menu.MenuItem - Creates a sub-menu (also known as a pull-right menu) which essentially pops up a new menu to the right of the currently displayed menu.Clicking on this arrow drops down a menu of items to add to the menu: As you move the mouse pointer over the Type Here field, the field will highlight and a down arrow will appear to the right of the Type Here text. This time, however, we are going to add a different type of item to the menu. Once again, Visual Studio provides the opportunity to add another item. Click in this field and enter &Read File. When you have entered this text Visual Studio will add another Type Here field beneath the "Open File" entry. Once the File menu item has been added, Visual Studio will create both a drop down menu and a field to add another menu item as follows:Ĭlick on the Type Here field and type &Open File. Windows indicates the accelerator for a menu item by underlining the letter (as you will see by the underlined 'F' on your File menu item).

For example H&elp will declare 'e' as the accelerator for this particular menu item. The location of the ampersand in the menu item Text property dictates the accelerator key. As such, pressing Alt+F when the application is running will be equivalent to clicking on the menu item with the mouse pointer. The ampersand (&) is added to instruct Visual Basic to use the 'F' at the beginning of the file as the accelerator key for this menu item. This has the effect of creating a menu item object labeled "File". Click in the Type Here field at the top of the form and enter &File. call the method recursively to extract further.Use the Properties panel to change the name of the Menu Strip object to mainMenu. if sub menu contain child dropdown itemsįoreach (ToolStripItem toolSripItem in mnuItem.DropDownItems) Private void GetAllSubMenuStripItems(ToolStripMenuItem mnuItem) This method is called recursively inside to loop through all menu items Private List GetAllMenuStripItems(MenuStrip mnuStrip)įoreach (ToolStripMenuItem toolSripItem in mnuStrip.Items) C# – Iterate Recursive Through all Menu Items in a MenuStrip Control public class MenuStripItems The following is a sample method to help you in iterating through menustrip items.You can use these methods in the same class or can be made as separate static utility methods as you like.I just made it as private methods inside a class. So, How to iterate recursively through all menu items in a menustrip control? Iterating Through Menustrip Items: There might be many cases and requirements to loop through all menu items in a menustrip Control.
