<Previous Lesson> <<Home>> <Next Lesson>
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 or 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 such as Microsoft
Words. What you need is to include those menu items that can improve the
ease of using your program by the user, and not to confuse the user with
unnecessary items. Adding menu bar is relatively easy to accomplish in Visual Basic. There are two
ways to add menus to your application, one way is to use the
Visual Basic's Application Wizard and the other way is to use the menu editor.
37.1 Adding Menu Bar Using Visual Basic's Application Wizard
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 menus into his or her application. To start using
Visual Basic's Application Wizard, you click on the Application Wizard
icon at the Visual Basic new project dialog box, as shown in Figure 37.1 below:

Figure 37.1: New Project Window
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.

Figure 37.2
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.

Figure 37.3
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

Figure 37.4

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

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:

Figure 37.7
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.
The complete code is as follows:
Private Sub mnuFileOpen_Click()
Dim sFile As String
With dlgCommonDialog
.DialogTitle = "Open"
.CancelError = False
Dim sFile As String
With dlgCommonDialog
.DialogTitle = "Open"
.CancelError = False
'ToDo: 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(*.*)|*.*"
.ShowOpen.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
Exit Sub
End If
sFile = .FileName
End With
'ToDo: add code to process the opened file
End Sub
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.

Figure 37.8
For example, selecting the
jpeg file will allow you to choose the images of jpeg format, as shown in Figure 37.9.

Figure 37.9
Clicking on the particular picture will load it into the image box, as shown in Figure 36.10 below

Figure 37.10
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:

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:
Figure 37.12
when you click Ok, the menu
items will be shown on the menu bar of the form.

Figure 37.13
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.

Figure 37.14
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.

Figure 37.15
Finally, you
can enter the code by clicking on any of the submenu items.
0 comments :
Post a Comment