Visual Basic 6.0 Guide
Making Menus

What Is A Menu?

There are several ways to implement menu systems in a program. You could have a series of command buttons where the user clicks the button that represents their choice. Users could be required to type a number into a text box to access their selected task.

Visual Basic programs are based on forms which, like all windows, tend to have their menus as clickable text just below the title bar.

Example Program With Menu

We will have to start by making a program with enough options to make a menu a worthwhile addition to the program. Our program will help a web designer to create HTML instructions quickly.

Short Explanation

HTML files are text files. To make text display a particular way, designers use tags to instruct the browser as to how the text should be displayed.

For example, <b>My Text</b> makes My Text appear in bold.

We want to make our program so that the user can highlight a few words, click on the relevant menu and have the instructions placed around the text we want to format.

Form Design

Start a new project and do the following things.

  • Name the form, frmMain and set its caption to Mini HTML Editor.
  • Add a text box which fills the form. Set its text property to nothing and the multiline property to true.. Name the text box, txtHTML.
  • Save Your Work and check that it looks like the example.

form design

Menu Design

We start by thinking about the way we want our menus to work.

Our program is going to be based on 3 functions. We will use the functions to alter the text that has been selected.

  • Make A Tag
  • Make A Custom Tag
  • Make A List

Our Menus will be as follows.

  • Format - Bold, Italic, Underline, Paragraph, Custom Tag
  • Lists - Bullets, Numbers

Visual Basic has a built-in Menu Editor. To access the menu editor, click on Tools, Menu Editor on the main menu.

menu image

  • To create our first menu, type Format in the box labelled Caption.
  • We also need to give this menu a name. Type mnuFormat in the Name box.
  • Click on the Next button and you should see the new menu in the text box below.
  • The grey shading will now appear underneath the word Format.
  • To show that the next item is to be a part of this menu, click on the right arrow.
  • For our first item in this menu, type the caption Bold and give this menu the name mnuFormatBold.

Continue the process with the other items for this menu using the names listed below for each of the captions,

  • Italic - mnuFormatItalic
  • Underline - mnuFormatUnderline
  • Paragraph - mnuFormatParagraph
  • Custom Tag - mnuFormatCustom

If you have done this correctly, the menu editor should now look like this.

menu editor image

  • To do our second menu, we need to click on the left arrow to show that the item that follows is a separate menu.
  • For this menu, type the caption Lists and name the menu mnuLists.
  • As before, click the right arrow to place the items that follow in the Lists menu.
  • There are two items on this menu. Bullets, which has the name mnuListsBullets and Numbers which has the name mnuListsNumbers.
  • When you have done this, click on the OK button to end the menu editing process.
  • Check that the menus appear on your form as you would expect.

Writing Functions

Remember those 3 functions that our program was going to be based on.

  • Make A Tag
  • Make A Custom Tag
  • Make A List

The Make A Tag function will be used to write tags for bold, italic, underline and paragraph tags.

The Custom Tag function will allow the user to input the letter(s) in an HTML tag and have the selected text altered accordingly.

The Make A List function will deal with the two different types of list in HTML.

Click on View, Code on the menu and type in the following function definition.

Private Function makeTag(strSelected As String, strTag As String) As String
makeTag = "<" & strTag & ">" & strSelected & "</" & strTag & ">"
End Function

Look carefully at the first line. This function has two parameters, strSelected and strTag. strSelected represents the text in the text box that has been selected. strTag is the letter (eg b) that is used to make the tag.

The Custom Tag function is as follows.

Private Function customTag(strSelected As String) As String
Dim strTag As String
strTag = InputBox("Enter Your Tag", "Mini HTML Editor")
customTag = "<" & strTag & ">" & strSelected & "</" & strTag & ">"
End Function

This time, the choice of tag letter comes from an inputbox.

The Make List function is as follows

Private Function makeList(strSelected As String, strTag As String) As String
Dim strTemp As String
strTemp = "</li>" & vbNewLine & "<li>"
strSelected = Replace(strSelected, vbNewLine, strTemp)
makeList = "<" & strTag & ">" & vbNewLine & "<li>" & strSelected & "</li>" & vbNewLine & "</" & strTag & ">"
End Function

A little bit more complicated this time.

Programming The Events

Now we have written the 3 functions, we need to make sure that clicking on the menu, makes the right thing happen.

To bring up the event procedure for the menu, you simply click on the item that you want to program. To begin, click on Format, Bold on the menu on your form.

You should see the following lines of code.

Private Sub mnuFormatBold_Click()

End Sub

We need to call our makeTag function and send it the relevant information. Alter this code to read as follows.

Private Sub mnuFormatBold_Click()
txtHTML.SelText = makeTag(txtHTML.SelText, "b")
End Sub

Use the same process to program the Italic event, this time writing,

Private Sub mnuFormatItalic_Click()
txtHTML.SelText = makeTag(txtHTML.SelText, "i")
End Sub

The full listing for all of the events programmed this way is as follows,

Private Sub mnuFormatBold_Click()
txtHTML.SelText = makeTag(txtHTML.SelText, "b")
End Sub
Private Sub mnuFormatCustom_Click()
txtHTML.SelText = customTag(txtHTML.SelText)
End Sub
Private Sub mnuFormatItalic_Click()
txtHTML.SelText = makeTag(txtHTML.SelText, "i")
End Sub
Private Sub mnuFormatParagraph_Click()
txtHTML.SelText = makeTag(txtHTML.SelText, "p")
End Sub
Private Sub mnuFormatUnderline_Click()
txtHTML.SelText = makeTag(txtHTML.SelText, "u")
End Sub
Private Sub mnuListsBullets_Click()
txtHTML.SelText = makeList(txtHTML.SelText, "ul")
End Sub
Private Sub mnuListsNumbers_Click()
txtHTML.SelText = makeList(txtHTML.SelText, "ol")
End Sub

Where To Go From Here

This program is only the beginning of something useful. Web designers often use expensive software to help them write their pages but even the most complete package misses out one or two things.

Many things are missing from this sample. For example, being able to load and save whole web pages rather than work with snippets of code would be a useful feature.