sas

Topic: Projects 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.   

m1

                                   Figure 1

m2

                            Figure 2

m3

                                               Figure 3m4

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

     The second form is  buttons.frm.   It contains an image box, two command buttons and a label.  The picture property of the image box is associated with a traffic icon which comes along with Windows 95.   The traffic icon contains three circles filled with red, green and amber colors respy.   By clicking the change button or any one of the colored circles, one colored circle can be highlighted.   The controls are arranged as shown in the figure 2 and the codes are entered as in the figure 2a in the code window of the buttons form.
               
Private Sub ChangeSignal()
    ' Check to see what color the light is, and then change
    ' it to the next color.  The order is green, yellow,
    ' and then red.
    If imgGreen.Visible = True Then
        imgGreen.Visible = False
        imgYellow.Visible = True
    ElseIf imgYellow.Visible = True Then
        imgYellow.Visible = False
        imgRed.Visible = True
    Else
        imgRed.Visible = False
        imgGreen.Visible = True
    End If
End Sub
Private Sub cmdChange_Click()
    Call ChangeSignal        ' Call the ChangeSignal procedure.
End Sub
Private Sub cmdClose_Click()
   Unload Me            ' Unload this form.

End Sub

Private Sub imgGreen_Click()
    Call ChangeSignal        ' Call the ChangeSignal procedure.
End Sub

Private Sub imgRed_Click()
    Call ChangeSignal        ' Call the ChangeSignal procedure.
End Sub

Private Sub imgYellow_Click()
    Call ChangeSignal        ' Call the ChangeSignal procedure.
End Sub

Figure 2a

     The third form  is check.frm.   It contains a frame containing two option buttons, three independent option buttons, two labels and one command button.   The captions are given as in the figure 3 and positioned.   The code  window for this form is opened  and the codes are entered as shown in the figure 3a.

' set up two string variables to hold the captions
Dim strComputer As String
Dim strSystem As String

Sub DisplayCaption()
    ' concatenate the caption with the two string
    ' variables.
    lblDisplay.Caption = "You selected a " & _
     strComputer & " running " & strSystem
End Sub

Private Sub cmdClose_Click()
    Unload Me   'unload the form
End Sub

Private Sub Form_Load()
    ' invoke a Click event in the default options
    ' to update the label caption
    opt486_Click
    optWin95_Click
End Sub

Private Sub opt486_Click()
    ' assign a value to the first string variable
    strComputer = "486"
    ' call the subroutine
    Call DisplayCaption
End Sub

Private Sub opt586_Click()
    ' assign a value to the first string variable
    strComputer = "Pentium"
    ' call the subroutine
    Call DisplayCaption
End Sub

Private Sub opt686_Click()
    ' assign a value to the first string variable
    strComputer = "Pentium Pro"
    ' call the subroutine
    Call DisplayCaption
End Sub

Private Sub optWin95_Click()
    ' assign a value to the second string variable
    strSystem = "Windows 95"
    ' call the subroutine
    Call DisplayCaption
End Sub

Private Sub optWinNT_Click()
    ' assign a value to the second string variable
    strSystem = "Windows NT"
    ' call the subroutine
    Call DisplayCaption
End Sub

     Figure 3a

     The fourth form is check.frm.   It contains two check boxes, one text box and two command buttons and one label.    They are captioned and positioned as shown in the figure 4.    The codes are entered in the code window as shown in the figure 4a.

                               
Private Sub chkBold_Click()
' The Click event occurs when the check box changes state.
' Value property indicates the new state of the check box.
    If chkBold.Value = 1 Then     ' If checked.
        txtDisplay.FontBold = True
    Else                          ' If not checked.
        txtDisplay.FontBold = False
    End If
End Sub

Private Sub chkItalic_Click()
' The Click event occurs when the check box changes state.
' Value property indicates the new state of the check box.
    If chkItalic.Value = 1 Then     ' If checked.
        txtDisplay.FontItalic = True
    Else                          ' If not checked.
        txtDisplay.FontItalic = False
    End If
End Sub

Private Sub cmdClose_Click()
   Unload Me    ' Unload this form.
End Sub

              Figure 4a

                            
     Save the Project and run the same and examine the output obtained in each of the forms.

Prev

 

 

 

 

 

 

 

 

 


footer back link


 

Error in my_thread_global_end(): 1 threads didn't exit