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.
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.
When you click on the VB Application wizard, the introduction dialog box will appear. 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. There are three choices of interface 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.
After clicking the Next button, you will be presented with a list of menus and submenus that you would like to add them to your application. Check to select a menu item and uncheck to unselect a menu item . Let say we choose all the menus and click next, then you will get an interface will File, Edit, View and Help menus. such as that shown in Figure 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.
Similarly repeat the process for having a MDI i.e. Multi Document Interface.
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:
Ex : Now, we can easily 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, 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
'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
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.
For example, selecting the jpeg file will allow you to choose the images of jpeg format.
Clicking on the particular picture will load it into the image box.
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.
You can then add in other menu items on the menu bar by following the same procedure, as shown in
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.
Creating the Interface.: The first step in building a Visual Basic application is to create the
forms that will be the basis for your application’s interface. Then you draw the objects that make
up the interface on the forms you create.
1. Adding a text box to the form. Double-click the toolbox’s textbox to create a text box with
sizing handles in the center of the form.
2. Adding a Command Button1 to the form. Click on button and draw button1 to form then the
button appears on form.
3. Repeat step 2 to add a Command Button2 to the form.
Setting Properties
The next step is to set properties for the objects. The properties window provides an easy way to
set properties for all objects on a form. For the Example 1, you’ll need to change three property
setting. Use the default setting for all other properties. Building and Construction Engineering Dept. Visual Basic
Note:
• The Caption property determines what is displayed in the form’s title bar or what text the
controls displays on a form.
• The TextBox’s Text Property determines what text (if any) the TextBox displays.
• The Name property identifies a form or control. It’s necessary only for writing code.
Object Property Setting
Form1
Name Form1
Caption Example1
Font Bold and size 12
Command Button1
Name Command1
Caption O.k.
Font Bold and size 12
Command Button2
Name Command 2
Caption Close
Font Bold and size 12
TextBox Name Text1
Text Empty
Writing Code:
The code editor window is where you write Visual Basic code for your application. Code consists of
language statements, constants, and declarations. To open the code window, double-click the form or
control for which you choose to write code, or from the Project Explorer window, select the name of a
form and choose the View code button.
In the Object list box, select the name of an object in the active form. Or double click of an
object.
In the procedure list box, select the name of an event for the selected object. The Click
procedure is the default procedure for a command button and the Load is default procedure for
a form.
An event procedure for a control combines the control’s actual name (specified in the name
property), an underscore ( _ ), and the event name. For example (Command1_click).
Type the code between the Sub and the End Sub statements.
Choose the command1 and type the following code:
Private Sub Command1_click ( )
Text1.text=”Welcome”
End Sub Building and Construction Engineering Dept. Visual Basic
Choose the command2 and type the following code:
Private Sub Command2_click ( )
End
End Sub
Note: The statement END used to close the program runtime.
Running the Application
To run the application, choose start from the run menu, or click the start button on the toolbar ,
or F5 Click the command button (O.k.) and see the “Welcome” displayed in the text box. Click the
command button (close) the end the program and return to the form window.
Saving a Project
Choosing save project from the file menu. Visual Basic will prompt you separately to save the form
and then the project.
Example 2-2: Design a form with three text boxes and one Command
Button. Write code in the Command1 (Execute). So when run project enter the Student Name in
TextBox (Txt1) and the Father Name in TextBox (Txt2). When click on Command1 (Execute)
replace the Full Name in the TextBox(Txt3).
Solution:
Building and Construction Engineering Dept. Visual Basic
Creating the Interface.:
1. Adding a Label to the form1. Double-click the Label’s Label to create a Label with sizing
handles in the center of the form1.
2. Repeat step 1 to add Label2 and Label3.
3. Adding a TextBox to the form1. Double-click the toolbox’s textbox to create a text box with
sizing handles in the center of the form1.
4. Repeat step 3 to add Text2 and Text3.
5. Adding a Command Button1 to the form. Click on button and draw Button to form then the
Button1 appears on form1.
Setting Properties
Object Property Setting
Form1
Name Form1
Caption Example1
Font Bold and size 12
Command Button1
Name Command1
Caption Execute
Font Bold and size 12
TextBox1 Name Txt1
Text Empty
TextBox2 Name Txt2
Text Empty
TextBox3 Name Txt3
Text Empty Building and Construction Engineering Dept. Visual Basic
Labe11
Name Label1
Caption Student Name
Font Bold and size 12
Labe12
Name Label2
Caption Student Name
Font Bold and size 12
Labe13
Name Label3
Caption Full Name
Font Bold and size 12
Writing Code:
Choose the Form1 and type the following code:
Private Sub Form1_load ( )
Txt3.text=Form1.width-100
End Sub
Choose the command1 and type the following code:
Private Sub Command1_click ( )
Txt3.text=tex1.text+ “ “+txt2.text
End Sub
Running the Application
To run the application, choose start from the run menu, or click the start button on the toolbar ,
or F5 Click the command button1 (Execute) and see the Full Name displayed in the TextBox3.
Saving a Project
Choosing save project from the file menu. Visual Basic will prompt you separately to save the form
and then the project.
Setting Properties
Object Property Setting
Form1
Name Form1
Caption Example1
Font Bold and size 12
Command Button1
Name Command1
Caption Execute
Font Bold and size 12
TextBox1 Name Txt1
Text Empty
TextBox2 Name Txt2
Text Empty
TextBox3 Name Txt3
Text Empty Building and Construction Engineering Dept. Visual Basic
Labe11
Object Property Setting
Form1
Name Form1
Caption Example1
Font Bold and size 12
Command Button1
Name Command1
Caption O.k.
Font Bold and size 12
Command Button2
Name Command 2
Caption Close
Font Bold and size 12
TextBox Name Text1
Text Empty
Name Label1
Caption Student Name
Font Bold and size 12
Labe12
Name Label2
Caption Student Name
Font Bold and size 12
Labe13
Name Label3
Caption Full Name
Font Bold and size 12
No comments:
Post a Comment