You've designed your program and it's a beauty: an introductory form to welcome the user, a data entry form to get data from the user, a summary form to display the data analysis results, and a logon form to connect to the Internet—it's all there.
Suddenly it occurs to you—aren't Visual Basic Windows projects organized into modules, classes, and forms? How does the code in one form reach the code in another—that is, how can the code in the analysis module read what the user has entered in the data entry form? It's time to take a look at working with multiple forms.
To see how to create multiple-form applications, and how to communicate between forms, create a new Windows application. I'll call this example Multiwindow, and you'll find it on the CD-ROM. When you create this application, it has one Windows form, Form1. To add another, select the Project|Add Windows Form item to open the Add New Item dialog you see in Figure 4.9; select the Windows Form icon in the Templates box and click Open. This adds a new form, Form2, to the project, as you see in the IDE in Figure 4.10.
Here, I'll add a text box, TextBox1, to Form2, as you see in Figure 4.10. When the user clicks a button in Form1, I'll read the text in that text box and display it in a text box in Form1. In Form1, I start by creating a new object of Form2 and calling it OtherWindow:
Place this declaration anywhere in the code for Form1, outside any procedure, like this:
Public Class Form1 Inherits System.Windows.Forms.Form Dim OtherWindow As New Form2() ⋮
(You can also select the (Declarations) item in the right-hand drop-down list box in the code designer to move to this area of your code automatically.) To display this new form as soon as the program starts and Form1 is displayed, double-click Form1 to bring up its Load event handler, which is called when Form1 is about to be displayed. You can show the second form with its Show method:
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load OtherWindow.Show() End Sub
Now add a button, Button1, to Form1, give it the text "Read text", and also add a text box to Form1. When the user clicks this button, we want to read the text in the text box in OtherWindow and display it in the text box in Form1. To do that, we can access the text box in the OtherWindow object as OtherWindow.TextBox1, and the Text property of that text box as OtherWindow.TextBox1.Text. To display this text in the text box in Form1, I can use this code (note that I also hide the second window with the OtherWindow object's Hide method):
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = OtherWindow.TextBox1.Text OtherWindow.Hide() End Sub
Now when the user types something into the text box in the second window—as you see in Figure 4.11—and clicks the "Read text" button, the second window disappears and the text from its text box appears in the text box in Form1 (see Figure 4.12). This example is a success.