8.
Project using Multiple Forms
In this lesson we will consider a project using four
forms. In VB, the Form is the
container for all the controls that make up the user interface. A form has a title bar , on which the form’s
caption is displayed. On the right side
of the title bar are three buttons-Maximize, Minimize and Close. Each form is designed to operate
independently of the others, and they can communicate via global
variables. In other situations one form
can be controlled from within another.
This means you can manipulate the properties of the controls in one
form, from within another form. But you cannot capture the events of one form’s
controls in another form.
Creating Forms:
When you open
the Standard Exe window, you get the first form entitled as form1. For adding a form select ‘Add Form’ from the Project menu and then
select the Form icon from the Add Form Dialog Window. The second form is added, entitled as form2. In a similar manner you can add any number
of forms. When the project is finally
saved, you will be presented with form save dialog for each form. You can give appropriate names for each of
the forms. Of course when the project
is subsequently opened, all the forms in the project are automatically
opened and also saved when the project
is saved.
Description of
the Forms:
The first
form called the main form contains the titles of the other projects to which
one can branch by clicking the appropriate button. The layout of the main form
(main.frm) is as shown in the figure
1. It contains a menu bar with two
titles, ‘File’ and ‘Options’. The File
Menu has only one item namely, ‘Exit’.
The Options menu has 3 items namely,
‘Test Buttons’, Check Box’, and
‘Option Buttons’.. It has 4
command buttons with their captions changed to ‘Test Buttons’, ‘Check
Box’, ‘Option Button’ and ‘Exit’
respectively. The codes of the
controls used in the main form are entered as shown in the figure 1a.

Figure 1
Private
Sub
cmdButtons_Click()
' invoke a Click event in the menu
mnuButtons_Click
End
Sub
Private
Sub
cmdCheck_Click()
' invoke a Click event in the menu
mnuCheck_Click
End
Sub
Private
Sub
cmdExit_Click()
Unload Me ' unload the form
End ' end the application
End
Sub
Private
Sub
cmdOption_Click()
' invoke a Click event in the menu
mnuOption_Click
End
Sub
Private
Sub
Form_Load()
frmMain.Height = 3600
frmMain.Width = 4965
End
Sub
Private
Sub
mnuButtons_Click()
' display the form
frmButton.Show
End
Sub
Private
Sub
mnuCheck_Click()
' display the form
frmCheck.Show
End
Sub
Private
Sub
mnuFileExit_Click()
' invoke a Click event in the command
button
cmdExit_Click
End
Sub
Private
Sub
mnuOption_Click()
' display the form
frmOptions.Show
End
Sub
Figure 1a
   |