Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

WORKING WITH MULTIPLE FORMS 63

Figure 2.9

Editing the Web form’s HTML code

Click F5 to run the application. When Internet Explorer appears, enter some values in the text boxes and check out the application. The Web application is functionally equivalent to the Windows loan application you developed at the beginning of this chapter. Yet its user interface runs in the browser, but the calculations take place on the server (the machine to which the clients connect to request the WebLoanForm.aspx Web page). Every time you click the Monthly Payment button on the page, the page is posted to the server. The browser transmits the values on the various controls back to the server. The server processes these values (actually, it executes the event handler you wrote) and creates a new page, which is sent to the client. This page includes the value of the monthly payment. Web applications are discussed in detail later in this book; with this example I wanted to demonstrate the similarities between Windows forms and Web forms and how the same code works with both types of applications.

Working with Multiple Forms

Let’s return to Windows applications. 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. In Chapter 4, we’ll explore in depth the topic of building Windows applications with multiple forms. In this chapter, we’ll build a simple example of a multiform application by combining the math and financial calculators we built earlier in the chapter.

The way to combine the two applications is to create a new form, which will become the switching point for the two calculators. The user will be able to invoke either of the two calculators by clicking a button on the new form. Let’s design an application that combines the forms of the two projects.

Start a new project and call it Calculators. The project’s form will become the switching point between the other two forms, and it’s shown in Figure 2.10. Start by renaming the new form from Form1 to CalculatorsForm. To design it, add two Button controls and name them bttnMath and

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

64

Chapter 2 VISUAL BASIC PROJECTS

bttnLoan. Then set their Text properties to Simple Math and Simple Loan, respectively. As you can guess, all you have to do now is add the code to invoke each of the existing forms from within each button’s Click event handler. Add a third button on the form, call it bttnGame, and later you can add an action game to the Calculators project.

Figure 2.10

The main form of the Calculators application

At this point, we must add the forms of the MathCalculator and LoanCalculator projects into the new project. Right-click the name of the project, and from the context menu, select Add Existing Item. In the dialog box that appears, select the item MathForm.vb in the MathCalculator project’s folder. Do the same for the LoanForm of the LoanCalculator project. The Calculators project now contains three forms.

If you run the project now, you will see the Calculators form, but clicking its button won’t bring up the appropriate form. Obviously, you must add a few lines of code in the Click event handler of each button to invoke the corresponding form. To display one form from within another form’s code, you must create an object that represents the second form and then call its Show method. The code behind the Simple Math button is shown in Listing 2.11.

Listing 2.11: Invoking the Math Calculator

Private Sub bttnMath_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnMath.Click Dim calcForm As New CalculatorForm

calcForm.Show() End Sub

The calcForm variable is an object variable that represents the CalculatorForm form of the Calculators application. The name of the form is actually used as a data type, and this requires some explanation. The form is implemented as a Class and therefore you create objects of this type.

The Dim statement creates a new instance of the form, and the Show method loads and displays the form. If you run the project now, you’ll see the main form, and if you click the first button, the math calculator’s form will appear. If you click the same button again, another instance of the form will appear. What can we do to prevent this? We would like to display the CalculatorForm initially and then simply show it, but not load another instance of the form. The answer is to move the declaration of the calcForm variable outside the event handler, into the Form’s declaration section. The variable is declared once, and all the procedures in the form can access its members. Variables declared in an event handler take effect only in the event handler in which they were declared, and that’s why at this point, every time you click a button, a new instance of the corresponding form is

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

WORKING WITH MULTIPLE FORMS 65

created and displayed. If the variable calcForm points to a single instance of the CalculatorForm, then the form will be displayed every time we click the Simple Math button, but no new instance of it will be created. You’ll find out more about the scope of variables in the following chapter.

When one of the two calculators is displayed, it doesn’t automatically become the active form. The active form is the one that has the focus, and this is the main form of the application. To work with a calculator, you must click the appropriate form to make it active. To activate the most recently displayed form from within another form’s code, we’ll use the Activate method of the Form object. Rewrite the Click event handlers of the two buttons on the form as shown in Listing 2.12 (the listing shows the entire code of the form, so that you can see the declarations of the two variables that represent the forms of the application).

Listing 2.12: The Calculators Project

Public Class CalculatorsForm Inherits System.Windows.Forms.Form

Dim calcForm As New CalculatorForm() Dim loanForm As New loanForm()

Private Sub bttnMath_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnMath.Click calcForm.Show()

calcForm.Activate() End Sub

Private Sub bttnLoan_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnLoan.Click loanForm.Show()

loanForm.Activate() End Sub

End Class

Notice the statement that declares the loanForm variable: the variable has the same name as the data type, but this is no problem. It goes without saying that the name of the variable can be anything. Our next task is to specify which form will be displayed when we start the application. Rightclick the Calculators project name and, in the context menu, select Properties. On the Calculators Property Pages dialog box (Figure 2.11) is a ComboBox named StartUp Object. Expand it and you will see the names of all the forms in the project. Select the name of form you want to appear when the program starts, which is the CalculatorsForm.

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. The button’s Click event handler is shown next:

Private Sub bttnGame_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnGame.Click bttnGame.Left = Rnd() * Me.Width * 0.8

bttnGame.Top = Rnd() * Me.Height * 0.8 End Sub

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

66

Chapter 2 VISUAL BASIC PROJECTS

Figure 2.11

Open the Project Properties dialog box to specify the startup object.

This subroutine manipulates the Left and Top properties of the control to move the button to a different position. The Rnd() function returns a random value between 0 and 1. To calculate the horizontal position, the code multiplies the random value by the width of the form (actually, 80 percent of the width). The vertical position is calculated in a similar manner.

Each Visual Basic project is made up of files that are all listed in the Solution Explorer window. Each project contains quite a few files in addition to the Form files, and they’re all stored in a single folder, which is named after the project. If you open the Calculators folder (Figure 2.12), you will see that it contains the CalculatorForm and LoanForm forms. These are copies of the original forms of their corresponding applications. When you add an existing item to a project, VB makes a copy of this item in the project’s folder.

Figure 2.12

The components of the Calculators project

To move a project to another location, just move the project’s folder there. To create a copy of the project, just copy the project’s folder to a different location.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com