DotNetWebTutor.com                                                    Link to Us

Menu Controls

visual basic 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
vb menu tab in toolbar
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

example menu in visual basic
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
  • Add a separator

separator

  • Type E&xit in the box below

file exit in menu
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

  • Save1
  • Save2

Now you have a submenu under Save:
visual basic submenu
images and shortcuts in menus
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
select resource dialogbox
imagetransparentcolor
If you are using your own files you may have to set the ImageTransparentColor
Shortcuts

  • Select a menu item
  • Click the shortcut property

menu shortcut keys in vb

  • Select Ctrl  and X for the exit shortcut

control + x shortcut key
context menu control
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 menu
Context menus do not have a top-level menu

  • Click the smart tag

smart tag for context menu

  • Select Edit Items

managerrendermode

  • 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

set context menu

  • Test it

When the user right clicks on the listbox your menu comes up
context menu example
vb menu