Visual Basic™ Framework

Software Eng.: Menus & Text Program

[ Previous ] [ Index ] [ Next ]

Creating Menus with Visual Basic

Table of Contents

 

Background Information

In this tutorial, you will use Visual Basic to create custom menus on the forms using the Menu Editor.  The menus and their contents are controls that can be customized by changing their properties, like any controls in Visual Basic.


NOTE

In this Menu and Text program, the user can right click and select the menu item to align text.


Back to top

How does this work?


ALERT

First of all, you have to record Start Time:  _____________


Initial Steps: Create form and open the Menu Editor

1.      Start Visual Basic by following the step below.

NOTE

At windows task bar, click ‘Start – Programs – Microsoft Visual Basic 6.0 - Microsoft Visual Basic 6.0’ menu item.

2.      Select ‘Standard EXE’. Click on ‘Open’ button. A blank form called ‘Form1’ will show on the screen.

TIP

Note: If the new form does not appear on the screen, select ‘New Project’ from the File Menu.

3.      At ‘Properties’ window, type “frmMenus” in the field of (Name). Type “Menus and Text” in ‘caption’ field.

4.      Select ‘SavefrmMenus form ‘As’ from the File Menu.

5.      Select your drive from the Drives List, and open the directory to save the project.  Type the filename for the form (e.g. ‘frmMenus.frm’), and select ‘OK’.

6.      To open the Menu Editor, first of all, select the “frmMenus” form.

7.      Choose ‘Menu Editor’ menu item from the ‘Tools’ menu, or use the toolbar Menu Editor  icon.  You will see the following window.



ALERT

After finishing the initial steps, record Quarter Completion Time:  _____________


 

How to create menu, menu item and submenu item using Menu Editor:

8.      Firstly, your goal is to create “Speed” top-level menu.

TIP

To create a top-level menu:

 

Ø      In the ‘Menu Editor’ dialog, key in “&Speed” in the Caption textbox.

Ø      Next, key in “mnuSpeed” in the Name textbox.

Ø      Key in “2” in Index field.  (At this point, you can ignore the subsequent fields.)

Ø      Click ‘Next’ button to proceed to create the next menu.

 

NOTE

Caption textbox is the menu name the user sees.

NOTE

Name textbox is the variable name the programmer uses.  Every menu must have a Caption and a Name.

NOTE

Good programming practice:  Prefixing menus with “mnu”, while submenu items prefix with “mnuitm” allows them to be identified easily.

ALERT

Common programming error:  Not providing a value for either the Menu Editor’s Name or the Caption text boxes is a syntax error.

ALERT

Common programming error:  Attempting to assign a shortcut key to a top-level menu is a syntax error.

 

9.      Secondly, your goal is to create “Scale” second level menu item.

TIP

To create a second level menu item:

 

Ø      In the ‘Menu Editor’ dialog, key in “Sca&le” in the Caption textbox.

Ø      Next, key in “mnuScale” in the Name textbox.

Ø      Press the right arrow key  to change the level of this menu to a lower level.  Note that this menu item is indented only once beneath the top-level menu.  (At this point, you can ignore the subsequent fields.)

Ø      Click ‘Next’ button to proceed to create the next menu.

10. Thirdly, your goal is to create “Medium” sub-menu item, expanded from Scale menu.

TIP

To create a third level sub menu item:

 

Ø      In the ‘Menu Editor’ dialog, key in “Medium” in the Caption textbox.

Ø      Next, key in “mnuitmMedium” in the Name textbox.

Ø      Press the right arrow key  to change the level of this menu to a lower level.  Note that this menu item is indented twice from the top-level menu.

Ø      Click on ‘Shortcut’ drop-down list. Select “Ctrl+M” list item.

Ø      [Optional] For this Medium menu item, check the ‘Checked’ box.

Ø      Click ‘OK’ button to close Menu Editor.

11. Test your program by clicking the Run icon [VB Run] on the Toolbar or selecting ‘Start’ from the Run menu.  Press ‘Alt+S’, follow by pressing ‘L’ key.  If any errors occur, correct them and start again.

12. To stop the program, click the End icon [VB Run] or select ‘End’ from the Run menu.


ALERT

At this check point, record Semi Completion Time:  _____________


Proceed to create menu items and submenu items with their shortcut keys:

Note that your goal in this section is to create five top-level menus.  The following figure shows the Menu Editor dialog corresponding to this ‘Menus and Text’ program.

 

 

13. Proceed to create the menu items as in the following list of items, which are the submenu items.

TIP

Separator line is created by placing a hyphen, -, in the Caption textbox.  Key in “mnuitmSeparator” in the Name textbox.

TIP

[Optional] Details on how to open Menu Editor. You may skip this tip if you know how to open Menu Editor.

NOTE

[Hints] Start creating ‘&Program’ top-level menu, follows by “&Run” and “S&top” menu item.  Make use of ‘Insert’ button and    arrow buttons.

TIP

[Optional] Details on how to create a top-level menu and menu item. You may skip this tip if you know how to create menus.

 

Program menu

·        Run                 Ctrl+R

·        Stop                Ctrl+T

 

Speed menu

  • Scale

·        Slow

·        Medium    Ctrl+M

·        Fast

·        <Separator Line>

·        Custom           Ctrl+C

 

Colour menu

·        Red

·        Blue

·        Black (with ‘Enabled’ checkbox is unchecked. This is to specifies grayed or disabled menu item)

 

Bell menu

·        On

·        Off (with ‘Checked’ checkbox is checked. This is an option to create a checked menu item)

 

Align menu

 

 

14. Test your program by clicking the Run icon  on the Toolbar or selecting ‘Start’ from the Run menu.  Select ‘Speed – Scale – Fast’ menu item.  If any errors occur, correct them and start again.

15. To stop the program, click the End icon  or select ‘End’ from the Run menu.


ALERT

At this check point, record 2nd Quarter Completion Time:  _____________


 

How to create pop-up menu using Menu Editor

 

16. In the form, create a label and update the label with the following properties.  This is to create a label displaying the word “TEXT”.

(Name)

:

lblText

Alignment

:

1 – Right Justify

BorderStyle

:

1 – Fixed Single

Caption

:

TEXT

Font

:

MS Sans Serif – Bold – Size: 12

 

 

TIP

[Optional] Details on how to create a label. You may skip this tip if you know how to create label.

 

17. Use the ‘Menu Editor’ dialog to create the following pop-up menu from which the user can select either: “Right, Center or Left” to justify the word “TEXT” of the label that you have created in the previous step.

NOTE

Note that a pop-up menu’s ‘Visible’ checkbox should be unchecked in the Menu Editor dialog. This is to hide the right-clicked menu items

TIP

[Optional] Creating pop-up menu is similar to details on how to create a top-level menu. You may skip this tip if you know how to create pop-up menu.

PopUp menu

·        Right (for these Right, Center and Left menu item, both ‘Enabled’ and ‘Visible’ checkbox are checked)

·        Center

·        Left

TIP

[Optional] Creating pop-up menu items (i.e. Right, Center and Left) are similar to details on how to create a second-level menu item. You may skip this tip if you know how to create menu item.


18. For this form, type in the following codes.  You may copy and paste only the bolded codes to the respective object and procedure.

 

Object: ‘(General)  -  Procedure ‘(Declaration)

' Demonstrating pop up menus

Option Explicit     ' General Declaration

 

Object: ‘Form’ - Procedure ‘MouseUp

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _

                         X As Single, Y As Single)

   ' When right button is clicked display Pop Up menu

   If Button = vbRightButton Then

      Call PopupMenu(mnuPopUp)

   End If

End Sub

 

Object: ‘mnuitmLeft’ - Procedure ‘Click

Private Sub mnuitmLeft_Click()

   lblText.Alignment = vbLeftJustify

End Sub

Object: ‘mnuitmRight’ - Procedure ‘Click

Private Sub mnuitmRight_Click()

   lblText.Alignment = vbRightJustify

End Sub

Object: ‘mnuitmCenter’ - Procedure ‘Click

Private Sub mnuitmCenter_Click()

   lblText.Alignment = vbCenter

End Sub

 

Object: ‘mnuitmStop’ - Procedure ‘Click

Private Sub mnuitmStop_Click()

    End

End Sub

 

19. Test your program.

 

TIP

In the program, right-clicking the form displays a pop-up menu from which the user can select Right, Center or Left to justify TEXT.


NOTE

In your program, other than press ‘Alt + F4’, two ways to stop the program are pressing ‘Alt + ___’ follows by ‘___’ key or ‘Ctrl + ___’.

ALERT

Congratulations! Record End Time:  _____________


 

Back to top

What next?

Try to make the program more robust by putting in more error checks in the codes, so that you will understand how to enhance menus application using Visual Basic objects.

 

Back to top

 

Last revised: 09 February 2004      16:00

 

If you spot an error, please contact sbho@mmu.edu.my   In addition, please contact me if you find some aspects of this task is not explained clearly so that I can improve this documentation.