Multiple Forms & Variables and Constants in Multiple-Form Projects
Multiple Forms & Variables and Constants in Multiple-Form Projects
MULTIPLE FORMS
The first form a project displays is called the startup form .
To add more forms to the project and display them as needed.
Creating New Forms
Steps for adding a new form to a project:
STEP 1: Select Add Form from the Project
menu.
STEP 2: In the dialog box, select the New
tab and indicate the type of form you want (Form, About Dialog, Splash screen, Web browser).
STEP 3: Click on Open .
The new form will display on the screen and be added to the Project Explorer window.
Switch Between Forms
While in design time, to switch between forms in two ways
- The Project Explorer window's View Form button and View Code button switch to the form or code of the module that is selected (highlighted).
- To switch between forms by clicking on the form name in the Project Explorer window and clicking the View Form button, or by double –clicking on a form name.
Each form module is a separate file and a separate entity. The code that exists in one form module is not visible to any other form module.
Note : Any procedure declared with the Public keyword is callable from other modules in the project.
Adding Existing Form File to a Project
Each form is a separate module, which is saved as a separate file with an .frm extension. All information for the form resides in the file, which includes the code procedures and the visual interface as well as all property settings for the controls.
Steps for adding an existing form to a project:
STEP 1: Select Add Form from the Project menu.
STEP 2: In the Add Form dialog box, select the Existing tab and locate the folder and file desired.
STEP 3: Click on Open .
Removing Forms from a Project
If you want to remove a file from a project, select its name in the Project Explorer window. You can then either choose Remove File from the Project menu or right-click on the filename to display the shortcut menu and choose Remove Filename.
The Show and Hide Method
The Show Method
General Form
FormName.Show [Style]
- The FormName is the name of the form you wish to display.
- The optional Style determines whether the form will display modeless or modal.
- The values for Style can be 0 (for modeless) or 1 (for modal)
- The default is 0
- VB intrinsic constants: vbModal and vbModeless.
Examples
- frmSummary.Show 1 ( Display the summary form, modal style)
- frmSummary.Show vbModal ( Display the summary form, modal style)
- frmSummary.Show vbModeless ( Display the summary form, modeless style)
- frmSummary.Show ( Display the summary form, modeless style)
VB executes the Show and pauses; it does not execute any further statements until the user responds to the modal form.
Conversely, when VB encounters a modeless show method, it displays the form and continues execution; if additional statements follow the Show, they will be executed immediately, without waiting for response from the user.
Hiding a Form
The Hide method is very similar to the Show method but is used to remove a form from the screen.
General Form
FormName.Hide
Example
frmSummary.Hide
The Load and Unload Statements
When you Show a form, the Load is done automatically. The only time you will code a Load statement is when you want to load a form, but not display it until later.
To remove a form from the screen, you can hide it or unload it. Hiding a form removes it from the screen, but the form still remains in memory and uses system resources.
General Form
Load FormName
Unload FormName
Examples
- Load frmSummary
- Unload frmSummary
The Me Keyword
Refer to the current form by using the special keyword
Me .
Examples
- Unload Me 'Unload the form that is currently executing code
- Me.Hide 'Hide the form that is currently executing code
Referring to Objects on a Different Form
Each form is a separate module with its own variables, objects, properties and code. But when you use multiple forms, the code in one module cannot “see” the objects in another module without some help.
To reference an object in another form module is to expand the reference to include the name of the form:
FormName.ObjectName.Property
Examples
- frmSummary.lblTotalAmount.Caption = mcurTotal
- frmSummary.Show vbModal
VARIABLES AND CONSTANTS IN MULTIPLE-FORM PROJECTS
Global Variables and Constants
If you want variables and constants to be accessible to more than one form in the project, they must be global variables. To declare global variables, use the Public
statement in place of the Dim statement.
The Public Statement
General Form
Public Identifier [As DataType]
Public Const Identifier [As DataType] = Value
The format of the Public statement is similar to the Dim and Const statements. If the data type is omitted for a variable, the type defaults to variant.For a constant, if you omit the data type, VB selects the type most appropriate for the value.
Examples
- Public gcurGrandTotal As Currency
- Public gPersonName (Defaults to Variant data type)
- Public Const gsngTaxRate As Single = .0825
- Public Const gstrCompany = “R 'n R - for Reading and Refreshment” (Defaults to String data type)
Static Variables
To declare variables at the procedure level is the Static
statement. Static variables retain their value for the life of the project, rather than being reinitialized for each call to the procedure.If you need to retain the value in a variable for multiple calls to a procedure, such as a running total, declare it as Static.
General Form
Static Identifier [As DataType]
The format of the Static statement is the same as the format of the Dim statement. However, static statements can appear only in procedures;Static statements never appear in the General Declarations section of a module.
Examples
- Static intPersonCount As Integer
- Static curReportTotal As Currency
- Static variables do not require a scope prefix, since all static variables are local.
Guidelines for Declaring Variables and Constants
1.To place all local declarations (Dim, Const, Static) at the top of a procedure.
2.To use named constants for any value that doesn't change during program execution.
3.To keep the scope of variables and constants as narrow as possible.
4.To consider making variables and constants local if possible.
5.If you need to keep the value of a variable for multiple executions of a procedure, but don't need the variable in any other procedure, make it Static.
6.If you need to use a variable both in a procedure and also in a second procedure called by the first procedure, declare the variable as local and pass it as an argument.
7.If you need to use a variable in multiple procedures, such as to add to the variable in one procedure and to display it in another, use module-level variables.
Comments
Post a Comment