Creating and Displaying
New Forms
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
specific 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
Explorer.
9
596906c09.indd 103 4/7/10 12:32:17 PM
104
LESSON 9 Creating and displaying new Forms
FIGURE 91
UNDERSTANDING CLASSES AND INSTANCES
When you add a new form to the project, youre really adding
a new type of form, not a new instance of that type. If you add
the
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 briefly now and explain them again in greater detail later in the book in the lessons in
Section IV.
A class defines the characteristics of any objects from that class. Your code can use the
new keyword
to create objects of the class. Once you define 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 define 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
FIGURE 92
596906c09.indd 104 4/7/10 12:32:18 PM

Get Stephens' C# Programming with Visual Studio® 2010 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.