Menu Controls

There are two menu controls
- MenuStrip Control
- Lets you build the menu on the top edge of a window
- ContextMenuStrip
- Lets you add a context-sensitive menu that appears when the user right-clicks something that has a context menu attached
Menu controls are in the Menu & Toolbars section of the toolbox
Example
- Start a new project
- Double Click the Menu Control to put a menu on your form
- Click in the menu and type in the Type Here
Once you add a menu Item the control lets you add another item beside it or below it
Beside it is another menu item, below it creates a submenu item
- Type &File in the first box
- Type &Save in the box below
You can use the dropdown list to the right of a menu item to add a:
- Menu item
- Combobox
- Separator
- Textbox
- Type E&xit in the box below
Using an ampersand adds a hotkey to the menu
Double Click the exit menuitem to add code to the event
Add the following code:
Me.Close()
You can double click any menu item to add code to run when the user clicks that menu item
Test it
If you put submenus in your submenus they will slide out horizontally
Add two menus Items under Save
Now you have a submenu under Save:
Images and Shortcuts in Menus
If you look at some other programs they have images and shortcut keys in their menus
Images in menus are handles by 4 properties
- Image
- ImageAlign
- ImageScaling
- ImageTransparentColor
Select the menu
Select the image property and click the ellipsis
All images used in a menu must be included in your project
Add your images to a folder called Resources (resources.resx)
ImageAlign determines where the image appears. MiddleCenter is most common setting
ImageTransparentColor lets you determine the color in the image to make invisible
ImageScaleSize defaults to 16,16
SizeToFit resizes the image to fix exactly
All backgrounds are set to 255,0,255
Example:
- Select the image property of the menu
- Select import
- Select an image from the Visual Studio Image Library
C:\Program Files\Microsoft Visual Studio 8\Common 7\VS2005ImageLibrary
You may have to unzip it
Click OK
If you are using your own files you may have to set the ImageTransparentColor
Shortcuts
- Select a menu item
- Click the shortcut property
- Select Ctrl and X for the exit shortcut
Context menu control
This works just like a regular menu
Example:
- Start a new project
- Drop a ContextMenuStrip control on your form
The form goes into edit mode
Context menus do not have a top-level menu
- Choose the type of item to add to the menu
- Change the text property of each
- Click OK to get out of the editor
- Add a listbox
- Select the listbox and change the contextMenuStrip property to the name of your menustrip
When the user right clicks on the listbox your menu comes up