Freetutes.com - Free Visual basic Tutorial website on the web

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > VB6 (Beginners Tutorial)

Previous Page | Table of Contents | Next Page

Sponsored Links

Working with Menus in Visual Basic

 
Windows applications provide groups of related commands in Menus. These commands depends on the application, but some-such as Open and Save -are frequently found in applications. Visual Basic provides an easy way to create menus with the modal Menu Editor dialog. The below dialog is displayed when the Menu Editor is selected in the Tool Menu. The Menu Editor command is grayed unless the form is visible. And also you can display the Menu Editor window by right clicking on the Form and selecting Menu Editor.

In the Menu Editor Window you can specify the structure of your menu by adding one command at a time. Each menu command has two compulsory properties.

  • Caption : This is the name that user sees on the application's menu bar

  • Name : This is the name of the menu command. This property doesn't appear on the screen, but this name is used program the menu command.

An expanded Menu Editor window.

An expanded menu

An expanded menu

The programmer can create menu control arrays. The Index TextBox specifies the menu's index in the control array.

The Menu Editor dialog also provides several CheckBoxes to control the appearance of the Menu.

Checked : This is unchecked by default and allows the programmer the option of creating a checked menu item( a menu item that act as a toggle and displays a check mark when selected. The following is a Check Menu items.

Enabled : specifies whether a menu is disabled or not. If you see a disabled command in a menu that means that feature is not available. The Visible checkbox specifies whether the menu is visible or not.

To add commands to the Form's menu bar, enter a caption and a name for each command. As soon as you start typing the command's caption, it also appears in a new line in the list at the bottom of the Menu Editor window. To add more commands click Enter and type the Caption and the Name.

Creating Menus

Open a new Project and save the form as menu.frm and save the project as menu.vbp.

Choose Tools ››› Menu Editor and type the menu items as shown below.

Caption
Name
File mnuFile
Open mnuOpen
Save mnuSave
Exit mnuExit
Edit mnuEdit
Copy mnuCopy
Cut mnuCut
Paste mnuPaste

Run the application by pressing F5. You can see that you can select a menu. (Download the source code)

Creating Pop-Up Menus

Nearly every windows application provides a context menu (or short cut menu) that the user can invoke by right clicking the Form or a control. This Pop-Up menu is a regular menu, but it is not anchored on the Form. It can be displayed anywhere on the Form. Pop-Up menus are invoked with the PopupMenu method. First, you create a menu as usual. Suppose you have designed the basic File and Edit menus for an application, and they are displayed on the Form as usual. To make the application a bit easier to use, you can also display the Edit menu as Pop-Up menu. If the Edit menu's name is mnuEdit, you can insert the following line in a control's MouseUp event:

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
PopupMenu mnuEdit
End If
End Sub

(The MouseUp event is used because, unlike the click event, it illustrates which button was pressed.)

If the right mouse button is pressed, the code calls the Form's PopupMenu method to display the Edit menu. The PopupMenu method is usually called from within TextBox and PictureBox controls, because these controls can carry out editing operations.

If you don't want the Edit menu to be displayed in the menu bar, still you must have the Edit menu there and what you have to do is making the Edit menu invisible.

Previous Page | Table of Contents | Next Page

 

 

 

   
Home | Link to Us | Partner Links | About Us | Contact Us
Copyright © 2007-2008 Freetutes.com | All Rights Reserved