Few applications are built on a single Form. Most applications use two, three, or more Forms, which correspond to separate sections of the application. In this section, we are going to build an application that uses three Forms and lets the user switch among them at will. You’ll see how to write an application that opens multiple windows on the Desktop.
VB6 at Work: The calculators Project
The Calculators project combines the two Forms we have developed in this chapter into a single application. We’ll create a new project, call it’Calculators, and add the two Forms we have already designed (the project on the <;:0 is called Cales and you’ll find it in the Cales folder). We’ll then design a third Form that will become . our switching point. The new Form will let us load both calculators, as shown.
To implement of the new, multi-window application, follow these steps:
1. Start a IU;W Standard EXEproject.
2. Choose Project > Add Form to open the Add Form dialog box. Click on the Existing tab, and locate the Form LoanCalc on your disk (or on the CD). ‘The LoanCalc Form is added to the current project, and its name appears in the Forms folder under the project’s name.
3. Choose Project> Add Form, and locate the Form MathCalc in the Existing . tab to add the Form to the project.
You now have a project with three Forms (the first one being an empty Form). If you run the application; you’ll see the following error message:
Must have Startup Form or Sub Main()
To specify an application’s start-up Form, do the following:
4. Open the FormI Form in design mode, select it with the mouse, and in the’ Properties window, set its Name property to Calcu lators and itS caption to Mastering VB6.
5. Now add four Command buttons to the Form, as previously shown.
The code behind the first two buttons should show the MathCalc and Loan- Calc Forms. To show a Form from within another, you must use the Show method, which has the following syntax where Form is the name of the Form you want to show:
Form. Show
6. Now, enter the following code in the Math Calculator button’s Click event:
The code behind the Play a Game button should also call the Show method of another Form, but it doesn’t. I regret not developing a game for your enjoyment, but I did implement a fun feature. When you click this button, it jumps to another place on the Form .
This subroutine manipulates the Left and Top properties, of the Command button to.move the button to a different position.
The last button, Exit, ends the application with the End statement:
Private Sub ExitButton_Click()
End
End Sub
This is all it takes to create a multi-window application based on some existing Forms. No changes in the existing Forms are required.
Now you can save the project. Choose File> Save Project As, and in the Save dialog box create a new folder and save the project in it.
On the CD, you’ll find the Calculators project in the Cales folder. You may notice that only the project file and the application’s main Form are stored in this folder. The Math and Loan Forms are still in their original folders. The structure of a Visual Basic project and how. its components are stored on disk are discussed shortly, in the section “A Project’s Files.”·
The Startup Object
Now press F5 or choose Start> Run to run the application described the previous section. Instead of running the application, Visual Basic displays the following’. error message:
Must have a startup form or Sub Main()
VisualBasic wants to know which Form to display when it starts. By default, Visual Basic displays the Form name of Forml or starts by executing a subroutine called Main. Our project has neither, so Visual Basic needs more information in order to start the application.
This’ is the only Form that can call the other two. Expand the Startup Object drop-down list and select Calculators. CIick OK to start the application. Now, run the application and see how it works. Click the first two buttons in the main window to display the Loan and Math Forms. Switch from one window to the other, and click the main Form’s buttons while the two windows are open. Visual Basic doesn’t open the corresponding window again, but moves the focus
to it (brings it on top of any other window on the Desktop).
to it (brings it on top of any other window on the Desktop). To close a window, click the Close button (the 4ttle Xbutton at the upper right of the window). You can close and open the Math and Loan windows as many times as you wish. If you close the application’s Main window, though, you’ll have to restart the application. This action is equivalent to clicking the Exit button of the main Form .