104
LESSON 9 Creating and displaying new Forms
FIGURE 91
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
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 92
596906c09.indd 104 4/7/10 12:32:18 PM