During this hour you will learn
As you build more advanced applications, you'll begin to add standard
forms to your applications that perform routine tasks. For example,
if you choose About from virtually any Windows application'sHelp
menu, a dialog box appears that describes information about the
application, such as contact information, and some system statistics
that users may find useful.
This hour's lesson describes some of the predefined forms that come with Visual Basic that you can use and customize so that your applications share these common dialog boxes with other applications. Often called form templates (a template is a pattern for something), these sample forms take the tedium out of manually adding such forms to your applications.
FAST TRACK |
Do you already work with multiple forms? If so, you may want to skip to Hour 33, "Developing and Designing Forms", to learn more about form design and usage. |
One of the most common dialog boxes that appears in almost all
Windows applications is the About dialog box, which pops up when
users choose About from the Help menu. Figure 26.1
shows the About dialog box that appears in VB.
VB's About dialog box describes the system.
This topic section examines the process needed to add one of the form templates to your project. These are the general steps you follow:
Adding Form Templates
When you use the Visual Basic Application Wizard to create a new project's shell, you see the dialog box shown in Figure 26.2. This dialog box lets you select up to four standard forms (also called form templates) in the project. As you'll see in this lesson, you don't need to run the VB Application Wizard to include these forms in your project because you can add them at any time.
You can add forms from VB's Application Wizard.
This lesson's final topic section discusses the form templates that appear when you click the wizard's Form
Templates command button.![]()
If you want to add one or more of these forms outside the wizard,
choose Add Form from the Project menu to display
the Add Form dialog box (see Figure 26.3). Then select one of
the form templates listed there.
You can insert a form template in your project at any time.
Changing the Form Templates
The form templates contain suggested controls that you can use. For example, the About dialog box template holds labels where you can place the application's title, version number, a description, and any warning text (such as copyright violation warnings) that you want your users to read. Also, the form template's About dialog box contains two command buttons and an icon.
In most cases, you should follow the suggested control layout. In other words, don't rearrange the labels so that the title appears beneath the description. The form templates are common dialog boxes that your users are used to seeing. Users will accept your application more quickly if you provide them with a standard set of auxiliary dialog boxes. If a particular form template control doesn't apply, you can delete that control. For example, you may not have any warnings to display, so don't include the warning Label control in your application's About dialog box.
Activating the Form Templates
Your application must be able to activate the form template when
needed. For example if you add an About dialog box, you'll certainly
want an About option on the Help menu. Even if your
application doesn't support online help, add the one Help
menu option so that users can display the About dialog box by
using a standard approach.
When working with a project that contains more than one form, use the
Window menu to switch between form windows that you want to edit.![]()
Use the Show method to display
a form template. For example, the form template's About dialog
box is named frmAbout. TheAbout
option's click event procedure might include the following single
statement to display the dialog box:
frmAbout.Show ' Display the About dialog box
Form Template Code
The form templates don't just provide guidelines for standard forms. The form templates also provide you with a lot of code that goes with the form. For example, the About dialog box comes into your application bringing with it five event procedures, plus a Declarations section in its form module.
The About dialog box contains a command button labeled System
Info. Clicking the button requests information on the current
hardware, such as memory and the processor type. You don't have
to write code to detect the hardware; the form template contains
all the necessary code to display users' computer hardware information
by using a standard hardware description window. Figure 26.4 shows
the System Information window that the form module's code produces
when you add the About dialog box to your application.
Code that comes with the About dialog box displays system information.
This example gives you practice in adding the About dialog box form template. Create a new project and follow these steps to hook up an About dialog box:
The About dialog box responds to the menu selection.
As you can see in Figure 26.5, the About dialog box knows the name of the project and displays the name in the About dialog box's title area. The About dialog box gets the name from the App object (see Hour 20, "Understanding Classes and Using the Object Browser," for an explanation of App's properties). The dialog box's Form_Load () event procedure contains the following code that initializes the title from the App object:
Private Sub Form_Load ()
Me.Caption = "About " & App.Title
lblVersion.Caption = "Version " & App.Major & "." &
[ccc]App.Minor & "." & App.Revision
lblTitle.Caption = App.Title
End Sub
You can set the application's major, minor, and revision numbers at runtime in the primary form's Form_Load () event procedure if you choose to use these values. You can also delete the About form module's reference to these values (and the labels that display the values) if you don't need version numbering.
The About dialog box's form module contains code that initializes these values (the title and version number) for you. The About dialog box, however, can't initialize such labels as the application's description or warning area. You have to initialize the description yourself in the description label's (named lblDescription) Caption property. If you don't need a warning or copyright area, delete the About dialog box's Warning label (named lblDisclaimer). You could, in its place, insert an icon or Picture Box control that grabs users' attention.
The About dialog box involves more code than just the application's
name and version numbers. Obviously, the About dialog box's form
module contains the code to look for the application's name and
version numbers. Also, the About dialog box's form module contains
code that unloads the About dialog box if users click OK. The
real power of the About dialog box, however, lies in the System
Info command button's event procedure:
Private Sub cmdSysInfo_Click()
Call StartSysInfo
End Sub
StartSysInfo is a general procedure listed below the event procedures in the About dialog box's form module. StartSysInfo's code runs a system program named MSINFO32.EXE located in your Windows directory. Although you could override this program with your own, why not stick with a standard system information program that users will see in other Windows programs?
If you were to rerun the program, display the About dialog box,
and click the System Info button, the System Information
application would start. (The application is known to be a child
process of your application. You application will continue
when users close the System Information window.)
Now that you've built a project that uses the About dialog box,
you'll have no trouble placing the other three primary form templates.
When you choose Add Form from the Project menu,
you have the chance to add several form templates to your current
project. This topic section looks at these three form templates:
Not all applications should include all the form templates. Most Windows applications do include the About dialog box, so you should make it a habit to include About in your applications. The remaining form templates may or may not fit into your application, depending on your application's goals and requirements. This topic section looks at these three standard form templates in detail, so you can determine whether you need to add one or all of them to your own projects.
Throughout this section, you'll learn how to connect the various form templates to your application. If you add one or more of these form templates while using the VB Application Wizard, the wizard will take some of the work off your shoulders so that you won't have to do as much to integrate the form templates into your project.
![]()
The Splash Screen
A splash screen is an introductory screen that displays an opening message and perhaps copyright and contact information about a project. (Although it's called a screen, the splash screen is actually another form window in your project's Forms collection.) The splash screen's primary purpose is to greet your users. Unlike the About dialog box, users won't see the application's splash screen again unless they run your application again.
The splash screen often displays a graphic image, along with introductory text. Figure 26.6 shows one splash screen that appears when you start the Enterprise Edition of Visual Basic. The splash screen contains an attention-getting graphic image and information about the product for the user.
Some VB users see this splash screen on VB startup.
A splash screen often goes away after a brief period. Generally, you can add a command button or check for a keypress so that users can get rid of the splash screen at their leisure. You also could use a Timer control (see Hour 23, "Enhancing Your Program") to display the splash screen for a fixed amount of time. A splash screen can mask a startup delay that might occur if the application initializes files and data by displaying a splash screen for a few moments while the initialization takes place.
The splash screen does pose one requirement that the About dialog
box didn't. You must tell your application to display the splash
screen before the normal form appears. Set the splash screen
as the startup form in the Properties dialog box (which you access
by choosing Properties from the Project menu). You
also must add a command button or a Timer control event to the
splash screen to display the next window when it's time to do
so.
The Login Dialog Box
As online computer use grows, the need for security grows with it. The Login dialog box is an interesting form template that you can add to your project that asks for the user name and password and returns the values to your application for processing. Figure 26.7 shows the Login dialog box.
Use the Login dialog box to request a user's name and password.
When users enter their name and password, the actual user name appears but the password appears as asterisks thanks to the dialog box's PasswordChar property. Although the asterisks appear as users enter the password (to protect the password from snooping eyes), your program will have access to the real password that's being typed. The initial password is password, and you can use it to test your program's Login dialog box. Listing 26.1 shows the form module's code behind the Login dialog box.
Listing 26.1 Login.bas: Letting Users Log In
Option Explicit
Public LoginSucceeded As Boolean
Private Sub cmdCancel_Click()
'set the global var to false
'to denote a failed login
LoginSucceeded = False
Me.Hide
End Sub
Private Sub cmdOK_Click()
'check for correct password
If txtPassword = "password" Then
'place code to here to pass the
'success to the calling sub
'setting a global var is the easiest
LoginSucceeded = True
Me.Hide
Else
MsgBox "Invalid Password, try again!", , "Login"
txtPassword.SetFocus
SendKeys "{Home}+{End}"
End If
End Sub
The form module uses a global variable named LoginSucceeded, which your code can for True or False on return from the dialog box. If users click the Cancel command button, the cmdCancel_Click() event procedure sets LoginSucceeded to False.
To adapt the code for your own users, follow these steps:
Just because Microsoft supplied the Login dialog box code with a global variable doesn't make the global good to use. As the cmdOK_Click() event procedure's remark explains, the global variable is the easiest way to inform the surrounding application the success of the login, but good programming practice recommends that you replace the global with local variables. Perhaps the best way to modify this code to improve its maintainability is to turn the subroutine procedure into a function procedure and setting the function's return data type to Boolean. The surrounding application can then test the function's return value for True or False.
![]()
The code at the end of the cmdOK_Click() routine might look confusing because it varies in style from what you're used to-plus you'll find a few new statements. Until now, MsgBox() has been a function, but the code contains this MsgBox statement:
MsgBox "Invalid Password, try again!", , "Login"
Although VB 5 still supports this MsgBox statement format, Microsoft is trying to get programmers to use the MsgBox() function. To turn this statement into a function, you need to assign the function to a variable (a Variant will do) and add parentheses like this:
varKeys = MsgBox("Invalid Password, try again!", , "Login")
The MsgBox statement can't determine which command button users clicked to close the message box. On the other hand, the MsgBox() function returns the button clicked. If OK is the only MsgBox() button you choose to display, you don't need to test for a button click because users must click OK to close the message box.
![]()
The next statement returns the focus to the Password text
box (this occurs only if users enter an invalid password) with
the SetFocus method. When
you apply SetFocus to a control
that can receive the focus, the application sets the focus to
that control. Although the focus might ordinarily move to another
control, such as the OK command button, the SetFocus
method moves the focus back to the Password text box because
users have to re-enter the password.
The final statement uses the SendKeys
statement to highlight the text that appears in the Password
text box. No matter what users type as the incorrect password,
the SendKeys statement moves
the text cursor to the beginning of the text box and then to the
end of the text box-in effect, highlighting the entire text box
contents so that the user's next keypress replaces the selected
text.
Hour 28, "Keyboard and Mouse I/O," explains the SendKeys statement in more detail.
![]()
The Options Dialog Box
The form template called the Options dialog box is perhaps the form template that does the least work but has the most potential uses. When you add an Options dialog box, you'll see the dialog box template shown in Figure 26.8. The dialog box contains four pages, with tabs at the top of each page and a frame on the body of each page. You can add pages and controls to the inside of the page frames for the options you require.
The Options dialog box displays pages for various options.
Many Windows programs contain an Options dialog box accessed from
the Tools menu that looks a lot like the Options dialog
box this form template produces. The Options dialog box is a starting
point, albeit just a dialog box shell, from which you can build
a more complete dialog box.
The Options dialog box uses a special ActiveX control called the
TabStrip control, which produces this multiple-page tabbed dialog
box. If you want to add a TabStrip control to one of your applications-that
is, if you don't want to use this form template-you'll have to
add the control from the Project Properties dialog box's Microsoft
Custom Controls 5.0 option (choose Properties from theProject
menu).
When you want to use the Options dialog box, follow these general guidelines:
Use the Property Pages dialog box to set up the dialog box pages.
The tbsOptions_Click() event procedure shows the appropriate page (and hides the other pages) in the TabStrip control as the program runs.
![]()
Follow these steps to create a project that contains a splash screen:
The splash screen is now modified.
Create a new project and run the VB Application Wizard. Accept
the application's default values until you get to the Standard
Forms dialog box. Click all four standard forms, click Next
twice, name the application Project26,
and click Finish. Visual Basic builds the application shell
while you wait. After the VB Application Wizard finishes, click
OK and read the setup instructions before closing the instructions'
dialog box.
As you might recall from Hour 2, "Creating Your First Application," the VB Application Wizard creates only a shell of an application. You must fill in the details. Nevertheless, the shell you now can study includes every one of the four standard form templates described in this lesson's first two topic sections.
To begin with, run the application to see how the application
has already created quite a bit for you. Don't enter a password
(the default password is blank until you add one to the code module),
but you can see that the splash screen grabbed the user's name
(your name in this case!) from the App
object and displayed the name automatically in the User
Name text box.
The splash screen appears but goes away quickly before the regular
form appears. The About dialog box appears when you choose About
from the Help menu, and the Options dialog box appears
when you choose Options from the View menu. This
project, although only a shell, gives you a lot of good code to
study when implementing your own application that requires one
or more standard form templates.
The form templates all reside in the \VB\Forms directory. This lesson describes the most common form templates, but you can create your own and add those forms to the \VB\Forms directory if you want more forms to be used as templates.
![]()
All the form templates described in the first two topic sections
appear as options in the VB Application Wizard. If you click the
Form Templates button on the wizard's Form Template dialog
box, the wizard lets you add these three additional forms (others
may appear if your \VB\Forms directory contains additional forms):
This topic section explains how to access these three additional form templates. Unlike the first two topic sections' forms, these three don't routinely appear in every application. You'll want to use these form templates only on special occasions and only if your application requires the features.
These extra forms don't appear just at the VB Application Wizard's prompting. Whenever you add a form file, Visual Basic gives you a list of form choices, and these three form templates will be among the choices Visual Basic gives you in the Add Forms dialog box. Depending on the forms listed in your \VB\Forms directory, you may have more form templates than those described in this lesson.
![]()
Tip of the Day
Do you sometimes start software and get greeted with a tip of the day? Windows 95 offers such a tip (see the Welcome Screen in Figure 26.11) until you turn off the option. Every time you start Windows 95, you'll see the tip until you deselect the check box and turn off the tip of the day.
The Windows 95 Tip of the Day dialog box offers help for newcomers.
When you add the Tip of the Day dialog box to a form window, Visual
Basic adds the Tip of the Day form to your Forms collection. Depending
on your screen size and default font settings, you may have to
extend the text box that holds the text labeled Show Tips
at Startup. Click under the Did you know... label to display the
label named lblTipText. This
label holds the tips that you display.
The code inside the form module sets up the form to display a daily tip each time users start the application. The following guidelines help you understand and modify the code to fit your application:
The Add-In Dialog Box
An add-in is a supplemental program to enhance the current application. Visual Basic supports add-ins that modify the development environment. For example, you may locate a VB add-in that adds a spell-checker to literals within VB code. VB contains a special feature called the Add-In Manager that manages these add-in programs.
If you need to supply an add-in manager to your own application, the Add-In dialog box gives you the place to start. Although the Add-In dialog box is fairly simple, the Add-In dialog box's form template gives you a start. When you add the Add-In form template to your Form window, you'll see the My Add In form in Figure 26.12.
The Add-In form template lets you specify new templates.
After you change the Add-In form's Caption
property, you must hook your add-in code to the form module. Writing
add-in code isn't a trivial task. You should get to know VB's
Add-In Manager (see the Add-Ins menu option) thoroughly
and master all of Visual Basic before trying to write your own
add-ins.
The ODBC Logon Dialog Box
ODBC stands for a special database access called Open Database Connectivity. ODBC provides a standard command set for accessing different kinds of data stored on different kinds of computers. The ODBC standard lets your program access data that it otherwise wouldn't be able to access. ODBC's goal is to let a number of different systems access data stored elsewhere.
Figure 26.13 shows the form template that appears when you insert the form on your Form window.
The ODBC dialog box form template lets you set up external database access.
The ODBC dialog box is a dialog box that you can add to your application so that users can select and access an ODBC database source. The source indicates the location and type of data the application is to access. Table 26.1 describes each ODBC field.
Table 26.1 The ODBC Dialog Box Text Boxes
Name | Description |
The Data Source Name that lists (in a drop-down list box) the currently registered ODBC sources in the user's system Registry | |
The User ID that supplies the connection with the user's identification so that the ODBC database can validate the user's access | |
The user's password to access the system | |
Data | The name of the database to connect to |
Dri | A drop-down list box that lists all the drivers on the system and lets users register a new ODBC driver |
The name of the server supplying the database if the DSN isn't available |
The code necessary to connect the ODBC dialog box's values to the proper ODBC-compatible database is fairly extensive. You must understand the ODBC API (Application Programming Interface) commands necessary to connect to the outside database. Such commands are beyond the scope of this book because Visual Basic programmers rarely have to resort to API-based routines, except in system-related applications.
As you've learned from this topic section, the extra form templates don't appear as routinely as the ones described by the first two topic sections. Nevertheless, your applications may require one or more of these form template services.
The Tip of the Day form template is perhaps the most common of the three described in this topic. Follow these steps to set up a Tip of the Day dialog box on your system:
Brush your teeth daily.
Save money for retirement.
Get plenty of sleep.
Read good books.
Exercise often.
Remember to floss.
Where did the Tip of the Day dialog box go? Although you've set up the tip file and added the Tip of the Day dialog box to your application, you must remember that VB first displays your project's primary form (called Form1 unless you change it) at startup. The following section explains how to set the Tip of the Day form for the initial form's startup and set up the code to display your regular form when users close the Tip of the Day dialog box.
From the Project menu choose Properties, and set
the Startup Object to frmTip.
Click OK to close the dialog box. The application is now set to
display the Tip of the Day dialog box when the user runs the application.
You must connect the regular form (named Form1 still) to the Tip of the Day dialog box form module so that the regular form appears when the Tip of the Day dialog box disappears. Change the cmdOK_Click() procedure as follows:
Private Sub cmdOK_Click()
Unload Me ' Unloads the tip dialog box
Form1.Show ' Show the regular form
End Sub
You must attend to one more item. If users decide not to see the tips in subsequent startup sessions, there's no way now for the tip's form module to display the regular form. Therefore, add the Form1.Show statement to the Form_Load() procedure as follows:
Private Sub Form_Load()
Dim ShowAtStartup As Long
' See if we should be shown at startup
ShowAtStartup = GetSetting(App.EXEName, "Options",
[ccc]"Show Tips at Startup", 1)
If ShowAtStartup = 0 Then
Unload Me
Form1.Show ' Show the regular form ** New statement
Exit Sub
End If ' Rest of code is not shown here
You now can run the application and read the tips randomly by
clicking the Next Tip command button. When you click OK,
the regular form appears, although the form is blank and boring
because you've added nothing to it. If users decide not to see
the tips in subsequent sessions, the application will show the
regular Form1 form at startup.
You can set up the application to display the tip or the regular startup form in other ways, as well. If you add proper Show methods to a subroutine procedure named Main, you can add this Main subroutine to the startup object.
![]()
The form templates exist to save you time. By adding them to your project, you won't have to take the time to create the forms and add the routine code that they need to integrate with your applications. The form templates contain all the code you need to begin connecting the form templates to your applications. You'll have to modify the code slightly and adjust the control properties that appear on the forms, but your job is made much lighter than would otherwise be the case without the form templates.
In Hour 27,, "Accessing Databases," you'll learn about VB's extensive set of data controls. The data controls let your applications access data that resides in outside database files.