vbi-62

  • (i) Start visual basic with standard new project.
  • (ii) Insert a label on the form and change its caption as “Hello Word”.
  • (iii) Now it is time to insert menu control using menu editor dialog box. Open the menu editor dialog box by clicking menu editor tool from standard tool bar, select menu editor option from tools menu or press CTRL+E. In all the three cases, the menu editor dialog box will open.
  • (iv) First, we will create the file menu. In the caption box, type &File. Press the tab key to move the focus to the name box. In the name box, type mnuFile.
  • (v) To add the next control, click the next button or press enter. Now we are ready to add other command in the file menu. Click the right arrow button to move our new control down one level in the hierarchy. This is because it is a command under the file menu. In the caption box, type E&xit, and in the name box, type mnuFileExit. In the shortcut drop down list, select a shortcut key. In this case, we select CTRL+X.
  • (vi) Now we ready to add another menu is the menu list. Click the next button or press enter key to create a new control. By default, the new control position in the menu system hierarchy is at the same level as the last control we added. The last control we added was the exit command on the file menu, show we will need to click the left arrow button to move the new control up one level. In the caption box type F&ont and in the name box, type mnuFont.
  • (vii) Now we will add the commands to the font menu. Click the next button or press enter key to create a new control. Click the right arrow button to move our new control down one level in the hierarchy. In the caption box, type “MS Sans Serif”, and in the name box type mnuFontSansSerif.
  • (viii) Click the next button to create the next control. Since this control is to also appear on the font menu, we do not have to change its level. In the caption box, type “Times New Roman” and in the name box, type mnuFontTimes.
  • (ix) Now we will add a separator bar to the font menu. Click the next button to create a new control. In the caption box, type hyphen (-) in the name box, type mnuFontSep1.
  • (x) Complete the font menu by adding the control.

Font Menu Controls

Caption

Name

Shortcut key

8 Point mnuFont8Point None
10 Point mnuFont10Point None
12 Point mnuFont12Point None
14 Point mnuFont14Point None
mnuFontSep2 None
&Bold mnuFontBold CTRL+B
&Italic mnuFontItalic CTRL+I

 

  • (xi) After we are done building the font menu, at the color menu control with the caption &Color and the name, type mnuColor.
  • (xii) Type the command for the color menu in the following format –

Color Menu Controls

Caption

Name

Shortcut key

&Red mnuColorRed None
&Green mnuColorGreen None
&Blue mnuColorBlue None
Blac&k mnuColorBlack None

 

  • (xiii) After we are done building the color menu, add the help menu control with the caption &Help and the name mnuhelp.
  • (xiv) Add control to the help menu with the caption &About and the name mnuHelpAbout.
  • (xv) Click OK button to save menu editor dialog box and exit from the menu editor dialog box.

VBO 62 Tool Box.PNG

Now we can add code event in following procedure –

(1) Start Visual Basic and open project which contains menu control.

(2) Change name of label box as lblMessage.

(3) Click on the exit button of file menu and write code in the code window.

Private Sub mnuFileExit_Click()
    End
End Sub

(4) Write the event procedure for control mnuFont, mnuFontBold, etc. in the following format –

Private Sub mnuFont8Point_Click()
    lblMessage.Font.Size = 8
End Sub

Private Sub mnuFont10Point_Click()
    lblMessage.Font.Size = 10
End Sub

Private Sub mnuFont12Point_Click()
    lblMessage.Font.Size = 12
End Sub

Private Sub mnuFont14Point_Click()
    lblMessage.Font.Size = 14
End Sub

Private Sub mnuFontBold_Click()
    If mnuFontBold.Checked = True Then
        lblMessage.Font.Bold = False
        mnuFontBold.Checked = False
    Else
        lblMessage.Font.Bold = True
        mnuFontBold.Checked = True
    End If
End Sub

Private Sub mnuFontItalic_Click()
    If mnuFontItalic.Checked = True Then
        lblMessage.Font.Italic = False
        mnuFontItalic.Checked = False
    Else
        lblMessage.Font.Italic = True
        mnuFontItalic.Checked = True
    End If
End Sub

Private Sub mnuFontSansSerif_Click()
    lblMessage.Font.Name = "MS Sans Serif"
End Sub

Private Sub mnuFontTimes_Click()
    lblMessage.Font.Name = "Times New Roman"
End Sub

(5) Write the event procedure for the command on the color menu. The code is as fallows –

Private Sub mnuColorRed_Click()
    lblMessage.ForeColor = vbRed
End Sub

Private Sub mnuColorGreen_Click()
    lblMessage.ForeColor = vbGreen
End Sub

Private Sub mnuColorBlue_Click()
    lblMessage.ForeColor = vbBlue
End Sub

Private Sub mnuColorBlack_Click()
    lblMessage.ForeColor = vbBlack
End Sub

(6) In the help menu, only one command is available. Most application have this command which display a dialog box know as about box. An about box usually shows some brief information about the application. Write the following code for the about command’s click event procedure.

Private Sub mnuHelpAbout_Click()
    MsgBox "Menu System Demo" & vbCrLf & "Design For Starting Out With Visual Basic 6.0"
End Sub

(7) Save and run this application and display menu control on the form.

vbo-62

Advertisements