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
Displaying Forms
105
rst form’s Red button, the event handler makes the first form red but the other forms are unchanged.
The code in the event handler is running in the first form’s instance so that’s the form it affects.
Hopefully by now you think I’ve beaten this topic into the ground and you understand the differ-
ence between the class (
MakeUserForm) and the instance (a copy of MakeUserForm visible on the
screen). If so, you’re ready to learn how to actually display forms.
DISPLAYING FORMS
The new keyword creates a new instance of a form. If you want to do anything useful with the form,
your code needs a way to refer to the instance it just created. It can do that with a variable. Im jump-
ing the gun a bit by discussing variables (theyre covered in detail in Lesson 11) but, as was the case
when I introduced the
if statement in Lesson 8, this particular use of the concept is very useful and
not too confusing, so I only feel a little guilty about discussing it now.
To declare a variable to refer to a form instance, you enter the form’s type followed by whatever
name you want to give the new instance. For example, the following code declares a variable named
newUserForm of type MakeUserForm:
MakeUserForm newUserForm;
At this point, the program has a variable that could refer to a MakeUserForm object but right now it
doesnt refer to anything. At this point the variable contains the special value
null, which basically
means it doesnt refer to anything.
To make the variable refer to a form instance, the code uses the
new keyword to create the instance and
then sets the variable equal to the result. For example, the following code creates a new
MakeNewUser
form and makes the
newUserForm variable point to it:
newUserForm = new MakeUserForm();
Now the variable refers to the new form. The final step is to display that form. You can do this by
calling the new form’s
ShowDialog or Show method.
Technically the variable doesn’t hold or contain the form. Instead it contains a
reference to the form. The reference is like an address that points to where the
form really is in memory. When your code says something like
newUserForm
.Show()
, it hunts down the actual form instance and invokes its Show method.
For now the distinction is small and you don’t need to worry too much about it,
but later it will be useful to know that some variables are value types that actu-
ally hold their values (
int, long, double) and some are reference types that hold
references to their values (object references and interestingly
string).
Lesson 17 says a bit more about this when it discusses structures.
596906c09.indd 105 4/7/10 12:32:18 PM
106
LESSON 9 Creating and displaying new Forms
The ShowDialog method displays the form modally. That means the form appears on top of the
program’s other forms and the user cannot interact with the other forms until this form closes.
This is the way dialogs normally work. For example, when you open the IDEs Project menu and
select Add Windows Form, the Add New Item dialog displays modally so you cannot interact with
other parts of the IDE (the Properties window, Solution Explorer, the menus) until you close the
dialog by clicking Add or Cancel.
The following code displays the form referred to by the variable
newUserForm modally:
newUserForm.ShowDialog();
The Show method displays the form non-modally. That means the form appears and the user can
interact with it or with the program’s other forms.
The following code displays the form referred to by the variable
newUserForm non-modally:
newUserForm.Show();
The UserForms example program shown in Figure 9-3 (and available as part of this lessons code
download at
www.wrox.com) displays a main form with a New User button. Each time you click the
button, the program displays a new
MakeUserForm. In Figure 9-3, you can see the main form and
two
MakeUserForms.
FIGURE 93
The following code shows how program UserForms displays a new MakeUserForm when you click its
button. The code declares a variable to refer to the form, creates the new form instance, and displays
the instance non-modally.
private void newUserButton_Click(object sender, EventArgs e)
{
MakeUserForm newUserForm;
newUserForm = new MakeUserForm();
newUserForm.Show();
}
596906c09.indd 106 4/7/10 12:32:18 PM
Controlling Remote Forms
107
Each time you click the button, the event handler executes again. Each time it runs, the event
handler creates a new version of the variable named
newUserForm, makes a new instance of the
MakeUserForm, and displays that instance, so each time you click the button, you get a new form.
FLOOD OF FORMS
The startup forms type Form1 is just like any other form type, so a program can
make new instances of it. That means you can create more forms that look just like
the startup form if you want.
However, though all forms look about the same to the user, the startup form has a
special position in the application. The program keeps running only as long as the
startup form exists. If you close that form all of the others close, too.
To avoid confusion, you should generally make the startup form look different
from other forms so the user knows that it is special.
CONTROLLING REMOTE FORMS
When you create a new form and make a variable to refer to it, you can later use that variable to
manipulate the form. There’s just one catch: the techniques described so far don’t keep the new form
variable around long enough to be useful.
For example, the following code defines the
newUserForm variable, makes it point to a new form,
and displays the form:
private void newUserButton_Click(object sender, EventArgs e)
{
MakeUserForm newUserForm;
newUserForm = new MakeUserForm();
newUserForm.Show();
}
When the code finishes executing the event handler, the event handler stops running. If the user
clicks the button again, the event handler springs back into action.
Unfortunately, when the event handler stops running, it loses its grip on the
newUserForm variable.
The next time the event handler runs, it creates a new variable named
newUserForm and works with
that one.
This is bad for a program that wants to manipulate the new form later. Because the variable is gone,
it cannot refer to it to manipulate the form.
The good news is that this is fairly easy to fix. If you move the variable’s declaration out of the
event handler, the variable exists throughout the programs lifetime. The event handler can make
the variable point to a new form, and it can then use the variable later to manipulate that form.
596906c09.indd 107 4/7/10 12:32:18 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.41.205