neronotes.blogg.se

Menustrip control
Menustrip control










  1. MENUSTRIP CONTROL HOW TO
  2. MENUSTRIP CONTROL CODE

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.

menustrip control

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.

menustrip control

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 “&File”. Add the high level menu item to the menu container ToolStripMenuItem fileItem = new ToolStripMenuItem("&File") į("First Menu Item") Create a top level menu item called "File" with "F" being the access key (Alt + f) NET Framework 4.5 but should even work in 2010. For clarity, and to avoid possible headaches, these examples were created with Visual Studio 2012 and. The below example will serve as the basis for further examples so keep it in mind.

No need to drag the MenuStrip control on the form at design time, this code will add it in at runtime instead. For simplicity we will simply wrap all these examples in functions so we can call them from a button click or any other type of event. Our first example below is a simple form menu we create from scratch.

That mechanism is outside the scope of our article here, but knowing how to build these menus dynamically can be useful for software developers. Then we could save that menu item on their system (in the form of a setting or even a piece of XML or whatever) and next time the program starts it would read that setting and recreate the menu for the user. This could be a menu item that your program can’t possibly know until the user creates it. Maybe we even allow them to assign their own accelerator key to it.

menustrip control

For instance, let’s say you want the user to be able to create a menu item, as a shortcut, for some particular operation. Well sometimes the menus have to change or be created based on program information. What we will cover this time around is just a few examples of how you can build these menus dynamically. Most of the time this is the way to go and works for the majority of programs out there. One of the easiest ways to build a menu on Winforms is by simply dragging and dropping a MenuStrip control from the toolbox on your form and then go to town filling in menu text and attaching events.












Menustrip control