Menu bar is the standard feature of most windows applications. The main purpose of the menus is for easy navigation and control of an application. Some of the most common menu items are File, Edit, View, Tools, Help and more. Each item on the main menu bar also provide a list of options in the form of a pull-down menu. When you create a Visual Basic 6 program, you need not include as many menu items as a full fledge Windows application. What you need is to include those menu items that can improve the ease of usage by the user. There are two ways to add menus to your application, using the Visual Basic's Application Wizard and or the menu editor.
The easiest way to add menu bar to your application is by using Visual Basic's Application Wizard. This wizard allows the user to insert fully customized standard windows menu into his or her application. To start using Visual Basic's Application Wizard, click on the Application Wizard icon at the Visual Basic new project dialog box, as shown in Figure 37.1 below:
When you click on the VB Application wizard, the introduction dialog box will appear, as shown in Figure 37.2. As you are not loading any default setting, just click on the Next button.
After clicking the Next button, the interface type dialog box will be displayed, as shown in Figure 37.3. There are three choices of interface available for your project. As we currently not creating a Multiple Document Interface (MDI), we choose Single Document Interface (SDI). You can also type the project name in the textbox below, here I am using MyFirstMenu.
Clicking the Next button wiill bring up a list of menus and submenus that you can add them to your application. Check to select a menu item and uncheck to unselect a menu item as shown in Figure 37.4. Let say we choose all the menus and click next, then you will get an interface comprises File, Edit, View and Help menus, as shown in Figure 37.5
When you click on any menu item, a list of drop-down submenu items will be displayed. For example, if you click on the File menu, the list of submenu items such as New, Open, Save, Save As and more will be displayed, as shown in Figure 37.6
Clicking on any of the dropped down menu item will show the code associated with it, and this is where you can modify the code to suit your programming needs. For example, clicking on the item Open will reveal the following code:
Now, I will show you how to modify the code in order to open a graphic file and display it in an image box. For this program, you have to insert a Image box into the form. Next add the following lines so that the user can open graphic files of different formats.
.Filter = "Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*. WMF|Jpeg Files(*.jpg)|*.jpg|GIF Files(*.gif)|*.gif|Icon Files(*.ico)|*.ico|All Files(*.*)|*.*".
Then, you need to load the image into the Image box with the following code:
Image1.Picture = LoadPicture(.FileName)
Also set the Stretch property of the Image box to true so that the image loaded can resize by itself. Please note that each menu item is a special control, so it has a name too. The name for the menu File in this example is mnuFileOpen.
Private Sub mnuFileOpen_Click()
Dim sFile As String
.DialogTitle = "Open"
.CancelError = False
'Set the flags and attributes of the common dialog control
.Filter = "Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*. WMF|Jpeg Files(*.jpg)|*.jpg|GIF Files(*.gif)|*.gif|Icon Files(*.ico)|*.ico|All Files(*.*)|*.*"
Image1.Picture = LoadPicture(.FileName)
If Len(.FileName) = 0 Then
sFile = .FileName
When you run the program and click on the File menu and then the submenu Open, the following Open dialog box will be displayed, where you can look for graphic files of various formats to load it into the image box.
For example, selecting the jpeg file will allow you to choose the images of jpeg format, as shown in Figure 37.9.
Clicking on the particular picture will load it into the image box, as shown in Figure 36.10 below
37.2: Adding Menu Bar Using Menu Editor
To start adding menu items to your application, open an existing project or start a new project, then click on Tools in the menu bar of the Visual Basic IDE and select Menu Editor. When you click on the Menu Editor, the Menu Editor dialog will appear. In the Menu Editor dialog , key in the first item File in the caption text box. You can use the ampersand ( & ) sign in front of F so that F will be underlined when it appears in the menu, and F will become the hot key to initiate the action under this item by pressing the Alt key and the letter F. After typing &File in the Caption text box, move to the name textbox to enter the name for this menu item, you can type in mnuFile here. Now, click the Next button and the menu item &File will move into the empty space below, as shown in Figure 37.11:
You can then add in other menu items on the menu bar by following the same procedure, as shown in Figure 37.12 below:
When you click Ok, the menu items will be shown on the menu bar of the form.
Now, you may proceed to add the sub menus. In the Menu Editor, click on the Insert button between File and Exit and then click the right arrow key, and the dotted line will appear. This shows the second level of the menu, or the submenu. Now key in the caption and the name. Repeat the same procedure to add other submenu items. Here, we are adding New, Open, Save, Save As and Exit.
Now click the OK button and go back to your form. You can see the dropped down submenus when you click on the item File, as shown.
Finally, you can enter the code by clicking on any of the submenu items.
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: firstname.lastname@example.org