ToolStripMenuItem col = new ToolStripMenuItem The first item is checked, the second item is disabled, the third item has our icon again and the last item is normal. In the process we also show you how you can control the state of an item as we create them. We use a for loop to create four menu items dynamically. You can build a menu based on an array or collection. We demonstrate this with another example below. Of course this can get a bit tiresome if you have a lot of options that are a bit repetitive or follow some kind of pattern that could probably make use of a loop to generate them. In the example above you can see that we create two sub-items one by one. Add the two items to the first sub itemį(subSubItem) į(subSubItem2) į(firstSubitem) ToolStripMenuItem subSubItem2 = new ToolStripMenuItem("Second Sub Sub Item") ToolStripMenuItem subSubItem = new ToolStripMenuItem("First Sub Sub Item") ToolStripMenuItem firstSubitem = new ToolStripMenuItem("First Sub Item", Image.FromFile("c:\\Delete.png")) Create our first sub item with a delete icon image This icon is then placed on the left side of the menu option. We also take the opportunity to show you how you can go about adding an icon to a ToolStripMenuItem to give more of a visual flair to the option. In our next example we demonstrate this nesting process by creating a menu which has some nested sub-items. Create the top menu, add some high level menus, add sub menus to those high level menus and even add sub menus to the sub menus. Among its versions is one which takes a typical string and one that takes another ToolStripMenuItem.Īs you can see here, building menus is very much a top down hierarchy. Each ToolStripMenuItem has this collection representing the sub-menu items it contains. We also create another menu item to fit under this “File” menu item using some simple text added to the DropDownItems collection. By putting it in front of a letter (in this case “F”) we are saying that if the user presses Alt F it will trigger the opening of this menu. The ampersand here denotes a quick access key. In this first example we create our MenuStrip object and attach a top level ToolStripMenuItem to it with the text “
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |