Objectives of the Week


Multiple Forms

There is no reason that your program has to be contained on a single form. There are a number of situations where your program would use different forms for different situations.

These are just a few examples of why you may want or need to develop your application using more than one form. This sequence assumes that you are adding a new form to an existing project and have already created the project directory.

To add an additional form to your project:

 

Save the Form


If you follow this sequence correctly, you will already be positioned in the Project Directory and the Dialog will already have the proper form name. All you have to do is click Save.

This process has completed two steps:

It is very important to distinguish between these two steps. The first performs a maintenance to the Project File. It makes a modification to the .VBP file in the project directory. This tells VB where to find the particular project component when you open the IDE. The second step is what creates the .FRM file in the project directory.

Take a moment to examine the project file with a text editor. If you have not already made the connection between the .VBP file and how it manages the project, this might help you understand it better.

The counterpart of "Add Form" is "Remove Form". This selection is also available under the Project Menu. Highlighting a form and removing it is only a maintenance to the project file. There will no longer be an association between the project and the removed form, but the component will still remain in the project directory until it is physically removed, perhaps using Windows Explorer.


Navigating Between Forms

A premise that I want to use for tonight, and probably for the entire course is that there is ALWAYS a main form that will invoke and control any other form. The one exception might be the Splash screen that we will soon discuss. Every other situation will involve the main form passing control to a subordinate form. The subordinate form will do it's job, and then return to the main form where execution will proceed with the instruction following the one that invoked it. There are other relationships that forms can have, but I want to stick with the Main - Subordinate relationship for right now anyway.

The model being described is calling the subordinate form as a subroutine. The Main form will invoke the subroutine or Modal form using the Show method:

Private Sub cmdSummary_Click()
        frmSummary.Show vbModal

End Sub

The Show Method passes control to the Summary form, and the vbModal parameter tells frmMain to wait until control is returned, and then resume with the instruction following the Show. This is exactly like a subroutine call. It is only when the Summary form is closed that control is passed back to the main.

The mechanics of this process can seem a bit complex as a number of new events are introduced. When any form, even the Main form is loaded, a series of events are fired. When Main "Shows" Summary, Summary fires the following sequence of events:

Initialize This only happens the very first time the form is referenced
Load Often used for initialization
Resize We will probably not use this event
Activate Also used for initialization
Paint Mostly used when we generate our own graphics
When the forms AutoRedraw Property is False, Paint does not fire

Depending on how the Subordinate form passes control to Main, subsequent calls to the Subordinate form could be different. This is due to two steps in displaying any form. The process involves loading a form into memory, and then displaying the form on the screen. In returning control back to the main, the subordinate can use either:

Using the Me.Hide instruction, the subordinate form is simply Deactivated. It remains in memory, so the next time it is called, only the Activate and Paint events are fired.

Unloading the form removes the form from memory, so the next time it is called, Load, Resize, Activate and Paint are fired in that order. Unloading a form fires the Query Unload and Unload events. Recall that Initialize is invoked the first time the form is referenced. The Terminate event, which is the counterpart to Initialize will fire when the program ends.

You can download this example to trace the events


What is "Me"?

Me is just a shortcut name for the current form. Any form method or property can use "Me" as the object rather than specifying the actual name of the form.

You can use either method of exiting the subordinate form. Unload will free up memory, but when you call the form a second or a third time, it will take longer to execute. Hiding the form leaves it in memory, so the following calls to display the subordinate form will be quicker. You also must make a decision with respect to initialization of variables used on the form as you have three "hooks" that allow you that opportunity.

Where you perform your initialization step will be influenced by how you return control back to the main form.


Modules

A Visual Basic Module is a component of a project that contains only code. There is no form associated with a module. This is a good place for variables that will be shared across the entire project, and for utility procedures that will be called from anyplace in the project.

 

Add a module to a project the same way you do a second form.

Use Project | Add Module.

Add a Module

 

Project.jpg (11554 bytes) Give the Module a name using the same type a standards you do for everything else. Three Character Prefix | Meaningful Name.

Click on the "Save" icon on the Tool Bar and you will be prompted for a name. You should already be positioned in the Project Directory and a default name based on the Module Name Property should make this process as simple as clicking on "Save"


Code Libraries

Code modules are available to handle a wide range of specialty functions. These modules are developed by individuals over time as part of "code reuse" libraries and are also available commercially. Using these modules is as easy as adding the module to your project and then educating yourself on the use of the subroutines and functions that are available in the code modules.

For example:

I have a module that contains "wrapper functions" for easy access to the Windows Registry
I also have a module that implements a simple form of encryption.

When ever I need encryption or registry services, I simply add the specific module to my project, and I can then call the functions that are contained in the code modules.

Remember that removing a module, like removing a form, does not delete the component. This process only updates the project file.


Public and Private Variables

Public and Private, like Local and Global, refer to the availability or scope of a variable.

Public means that the variable is to be shared with other modules. Public and Private are coded for variables declared at the module level. They don't make much sense at the event handler level as they are local to the event handler and can't be shared.

Dim means Private. If you want to share a variable, you must declare it Public. Form Controls are Public. Any control on a form is accessible by any code in the project.

The key to this access is to fully qualify the reference to the variable. This is the same Object.Property syntax used when referencing control properties. You can consider a variable declared as Public in a form to be a property of that form.

For example

This variable is declared in frmDate

Public strDayOfWeek As String

It can be referenced from frmMain by fully qualifying the variable name

lblDate = frmDate.DayOfWeek

frmDate is the Object. The Public variable DayOfWeek is the Property

Private in a Module means that only the code in that module can access the variable.


Static Variables

Static variables are local in scope, so variable is protected from global changes, but variable "remembers" its value.

In this example, intCount cannot be modified by any other event handler as it is a local variable, but unlike non-static variables, it will retain its value between calls to this event handler.

Private Sub Form_Activate()
    Static intCount As Integer

    intCount = intCount + 1

    lblCount.Caption = "Dates Converted: " & FormatNumber(intCount, 0)

End Sub


Adding Forms using a Wizard

When you select Project | Add Form, you are offered the assistance of a Microsoft Wizard. These forms are pre-configured models of forms that are used for specific purposes. These are skeleton forms that leave you with filling in the specifics.

About Wizard

Take a moment and add a couple of these to your projects. In most cases, I think you will find that it is easier to build your own. Whatever you think of the wizards, for this class, Do It Yourself.


Splash Screen

Why a Splash Screen?

In short, because most applications take WAY too long to load. A Splash Screen is a diversion. It distracts you, and you THINK that the application is doing something.

How long does it take before you start wondering if the application has stalled?

The Splash Screen satisfies your impatient mind.

It's also a chance to display some information about your program. It's easy to code too.

Take a look at this example. This code can be placed in any Main() procedure. Pass the name of the Splash Screen and the Main Form and this code will direct the traffic.

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

The Sleep Function is invoked as a Windows API call. More on this towards the end of the semester

Private Sub Main()
        Splash frmMain, frmSplash, 3

End Sub

Private Sub Splash(MainForm As Form, SplashForm As Form, Time As Integer)
        Load SplashForm
        SplashForm.Show
        SplashForm.Refresh

        Load MainForm
        Sleep Time * 1000
        Unload SplashForm

        MainForm.Show


End Sub

One additional step is to change the Project Properties and change the Startup Object to point to the Main subroutine in the module that in this case drive the Splash Screen logic.

Project Properties

 

Here's the project that makes use of this code


Build the Example

Step by Step through the First Example                    

Make sure that you:


Lab 1 -From the text - Page 243, VB Mail Order

Step by Step through the First Lab

Let me know when you are done and I will check you off as Complete

Lab 2  - From the Text - Page 244, VB Auto Center

You are on your own.


Assignment

Reading Assignment

Programming Assignment

As Always: The assignment is due before the start of class next week

Project Name

    Page 241, Problem 6.1

Easy modification to Flag Viewer from 2.2 and 5.5

Here is a list of the Program Grading Criteria