Multiple Forms and Menus Multiple forms Creating new forms - under Project select Add form Removing a form - under Project select Remove form Hide and Show Methods When running one form you may wish to display another form. - call a new form - put Show method in the code for the original form - return to the original form - put the Hide method in the code for the Exit button on the new form.
Example P. 163, 187 Private Sub mnuFileNewCustomer_Click() ‘ Display the billing form frmSummary.Show End Sub Example P. 164, 189 Private Sub cmdOK_Click( ) ‘ Hide the summary form frmSummary.Hide End Sub Referring to objects on a different form - general syntax is FormName!VariableName Example P. 164 frmSummary!lblTotalAmount.Caption = mcTotal Example P. 187 Private Sub cmdComplete_Click ( ) frmMain!lblCupCount.Caption = piCupCount
Standard Code Modules A standard code module is a piece of code which is used by several forms. - create a standard code module - under Project select Add Module - type in the code. - save the code (This creates a file with the extension .BAS ) Variables and constants in multiple forms projects Definition A global variable is a variable which is defined in the whole project (on every form). A module level variable is a variable which is defined in every procedure or function on one form. A local variable is a variable which is defined in one procedure or function.
Note The scope of a global variable is all the code in the entire project. The scope of a module-level variable is the code associated with one form. The scope of a local variable is the procedure in which it is defined, along with any procedure to which it is passed. Global variables and constants In the variable declaration, replace Dim by Public . Also, use a prefix of p to indicate that the variable or constant is Public. Example P. 166 Public pcGrandTotal As Currency Public Const psTAX_RATE = .2 As Single
Static variables Definition A static variable is a local variable which retains its value between procedure and function calls. Example P. 167 Static iPersonal As Integer Guidelines for variables and constants P. 168 Keep the scope of each variable as narrow as possible. (a) When you have a choice, make variables and constants local. (b) If a variable is used in several procedures on one form, make the variable module-level. (c) If a variable is changed on more than one form, make it Public. If a variable is referred to on more than one form but computed on only one form, make it module level. (d) Put Public variables in a standard code module.
An “About” Box - a form containing some information and (usually) an “OK” button Example P. 169 Menus The Menu Editor is a dialogue box which allows you to create menus. (P. 170 Fig. 6.6) There are two ways to call the menu editor. Method 1 Under Tools select Menu Editor . M ethod 2 Right-click on the form and select Menu Editor . The Menu Editor The Caption box lets you create the caption on the form. The Name box lets you name the menu item for the code. - names are preceded by mnu, the name of the menu, and the name of the item
Example Captions Names &File mnuExit &Exit mnuFileExit Creating a list - create the first menu item - create the second menu item and hit the right arrow key Example To create File Exit (1) type Caption File Name mnuFile (2) type Caption Exit Name mnuFileExit Hit the right arrow key.
Separator bar This is a line in a menu which divides items into separate categories. (You can see this in each of the Visual Basic menus.) - in Caption type - (the hyphen symbol) - in Name type any name List box This is the box at the bottom which displays the items which you have created. Submenus - create a menu item - hit the right arrow twice Checked and enabled
Creating a menu P. 172 - 174 Step 1. Display the Menu Editor Step 2. Type Caption and Name for the first menu. Step 3. Click on Next . Step 4. Click on the right arrow key (to indent). Step 5. Type in the menu items. For a submenu, click the right arrow key twice. Modify the menu - use the Menu Editor Code for the menu items - as for any other control Toggling check marks on and off - in code, use an If statement Example P. 175
Standards for Windows menus - include keyboard access commands - organize menus as Windows and Macintosh do Common dialog boxes - place the Common Dialog icon into the toolbox - under Properties select Components - select the dialog box option - from the toolbox, place a common dialog box onto the form One common dialog box allows you to display any of the dialog boxes in Visual Basic. Syntax - general object . ShowMethod - Example dlgCommon.ShowColor ‘Display color palette
Using information from the Color dialog box Example P. 178 In the code dlgCommon.ShowColor The Color dialog box appears, and the user selects a color . frmMain.BackColor - dlgCommon.Color Example P. 178 Using the Font dialog box Setting current values Do this before using a dialog box.
Programming Hints Setting the startup form - under Project select Properties - select the General tab - go to Select startup and highlight the desired form Adding an existing form - under Project select Add form Maximizing the window - in the Properties window set Windowsize to Maximize