Creating and Displaying
Most of this book so far has dealt with building forms. Previous lessons explained how to add,
arrange, and handle the events of controls on a form. They’ve explained how to work with
speciﬁc kinds of controls such as
MenuStrips, ContextMenuStrips, and ToolStrips. Using
these techniques, you can build some pretty nice forms that use simple code to manipulate
properties. So far, however, you’ve only learned how a program can use a single form.
In this lesson you learn how to display multiple forms in a single program. You see how to
add new forms to the project and how to display one or more instances of those forms. Once
you’ve mastered these techniques, you can make programs that display any number of forms
for all kinds of different purposes.
ADDING NEW FORMS
To add a new form to a project, open the IDE’s Project menu and select Add Windows Form to
see the dialog shown in Figure 9-1.
Leave the Windows Form template selected, enter a good name for the new type of form,
and click Add. After you click Add, Visual Studio adds the new form type to the project.
Figure 9-2 shows the new form in Solution Explorer.
Now you can add
Labels, TextBoxes, Buttons, MenuStrips, and any other controls you like
to the new form.
Remember, to open a form in the Form Designer, double-click it in Solution
596906c09.indd 103 4/7/10 12:32:17 PM
LESSON 9 Creating and displaying new Forms
UNDERSTANDING CLASSES AND INSTANCES
When you add a new form to the project, you’re really adding
a new type of form, not a new instance of that type. If you add
MakeUserForm type to a project and then run the program,
you still only see the original startup form (with the catchy name
Form1) and MakeUserForm is nowhere to be seen.
Form types such as
Form1 and MakeUserForm are classes. They’re like blueprints for making copies
of the class called instances. These are important and sometimes confusing topics so I’m going to
explain them brieﬂy now and explain them again in greater detail later in the book in the lessons in
A class deﬁnes the characteristics of any objects from that class. Your code can use the
to create objects of the class. Once you deﬁne the class you can make as many copies — instances —
as you like, and every copy is identical in structure to all of the others. Different instances may have
different property values but their overall features are the same.
For example, suppose you deﬁne a
MakeUserForm that has FirstName, LastName, Street, City, State,
and ZIP labels and textboxes. Now suppose your program displays two instances of this class. Both of
the forms will have the same labels and textboxes, so they have basically the same structure. However,
the user can type different values into the two forms.
Your code can also change different instances in various ways. For example, menu items, buttons,
and other controls could invoke event handlers that modify the form: change its colors, move controls
around, resize the form, or whatever. Here’s one of the more potentially confusing features of classes:
the code in the event handlers modify the form that is currently running the code.
For example, suppose you build a form that has three
Buttons that change the form’s BackColor prop-
erty to red, green, and blue, and then you display three instances of the form. When the user clicks the
596906c09.indd 104 4/7/10 12:32:18 PM