How to make a menu: Part one

Firstly, to start off, open up Visual Basic 6.0.

When you've done that, it should prompt you with a dialog box asking you what type of project you want to make. Choose Standard EXE and press open.

First Step.

Once you have done that, you should have something that looks like this:

Step 2.

Now we are going to set up the design of the application. To do this we are going to need one menu which this tutorial will be based upon.

Right click on the form. A menu should come down, click on "Menu Editor"

Step 3

Step4.2

Now we have the main Menu editing dialog open. This can control the style of our menu. So to give you a general idea of how to make a menu, we'll make a simple File menu. So in the caption textbox, enter the text, "&File". This means that the name of the menu that you click on will be called File and the "&" symbol means that when you hold down the Alt button and press F; the file menu will open. We could change it to: "Fi&le" and when we press alt+L then the file menu will open. Now we need to give it a name that we can use in code. So click down into the Name textbox and enter, "mnuFile".

Now click the in the white File space under the highlighted blue segment, this should turn blue now instead of the file piece:

Now click the arrow button that points to the right. Four dots should appear:

Step 5

What this is telling the application is to create a sub-menu. Or a button that is under the file menu. On most applications it would probably be, "New", "Print", "Exit"; things like that. So now we will give it a name. We'll make a New menu item. We'll make this button load a new form.

So do what we did before and give this item a name. So in the caption area, enter New and in the name area enter mnuFileNew.

Repeat the above steps: Click in that white space, click the arrow, and change the caption to Exit and the name to mnuFileExit. If you want you can add the "&" character to these names as well.

Step 6

You can click OK now. You should have a little menu on your form containing all the menus we added.

Step 7

Now for the coding!

Click on the file menu and then click on New. It should take you to a blank screen where you can write:

It should already have text there:

Private Sub mnuFileNew_Click()
End Sub

In the middle of this, insert this code

Dim intX As Integer

Dim frm1(1 To 1) As New form1

For intX = 1 To 1
frm1(intX).Show
Next

You should now have this code

Private Sub mnuFileNew_Click()
Dim intX As Integer

Dim frm1(1 To 1) As New form1

For intX = 1 To 1
frm1(intX).Show
Next
End Sub

What this is telling the application to do is, when you press the menu button for New, it is creating a new form. Where it says: "Dim intX As Integer" this means that IntX is going to be a number and later on it's used as a number that tells the application what number form it is.

Then it dims frm1 as a new instance of the form we are working on which is Form1.

Then it basically tells the application to show the new version of the form.

Now we need to code the Exit button. This is very easy code. Right click on the place where the code is (not on the text), and press Hide. You should be taken back to the design area of the form.

Click on File and then Exit (on your form, not Visual Basic! :P) You should have this code:

Private Sub mnuFileExit_Click()

End Sub

Now in the middle of this, type in: Unload Me

This tells the application to unload the current form. If you want to end the application entirelly, you would type in the word "End".

Hit F5 (or push the button below) and try out your new menu!

When you are done with your application, exit out of it and save.

 

Download Project | Back to tutorials | Go to part Two

 

Comment on this article:

Admin

Name: *
Email:
Homepage:
Message: *