108
LESSON 9 Creating and displaying new Forms
The following code demonstrates this technique. The main form’s Load event handler creates and dis-
plays a new
ColorForm. When the user clicks the main forms Red button, its event handler changes the
remote forms
BackColor and ForeColor properties. The startup form also contains green and blue
buttons that have similar event handlers.
// The remote form we will manipulate.
ColorForm remoteColorForm;
// Create and display the remote form.
private void Form1_Load(object sender, EventArgs e)
{
remoteColorForm = new ColorForm();
remoteColorForm.Show();
}
// Make the color form red.
private void redButton_Click(object sender, EventArgs e)
{
remoteColorForm.BackColor = Color.Red;
remoteColorForm.ForeColor = Color.Pink;
}
Here the remoteColorForm variable is declared outside of the event handlers. The forms Load event
handler initializes the variable and displays the remote form. The
redButton_Click event handler
uses it. Because the variable is declared outside of the event handlers, they can all use it. (Lesson 13
has more to say about when and where variables are available to the code.)
In the previous example, I moved the code that creates the ColorForm into the
main form’s
Load event handler. If that code stayed in a Button’s Click event
handler, the user could click it a bunch of times and create many different
forms. Each time the program created a form, it would make the variable refer
to the new one so it would “forget” the previous form. Then the Red button
would affect only the most recent form and not any others.
The RemoteForm example program shown in
Figure 9-4 (and available as part of this lesson’s code
download at
www.wrox.com) uses similar code to make
its
ColorForm red, green, or blue.
In addition to modifying a remote form’s properties,
you can change the properties of the controls on
that form. You refer to a control by using the
form variable, followed by a dot, followed by the
control’s name.
FIGURE 94
596906c09.indd 108 4/7/10 12:32:19 PM
Try It
109
For example, the bold line in the following code accesses the form referred to by the remoteColorForm
variable. It locates that form’s
messageLabel control and changes its Text property to “Im red!”
private void btnRed_Click(object sender, EventArgs e)
{
color_form.BackColor = Color.Red;
color_form.ForeColor = Color.Pink;
color_form.lblMessage.Text = “I’m red!”;
}
There’s one small catch to this technique: by default the controls on a form are private so no other
form can peek at them. You can easily fix this by setting a control’s
Modifiers property to Public.
Now the other form can see the variable and change its properties.
Controls on a form are private to prevent other pieces of code from accidentally
messing them up. By making a variable public, you remove this safeguard. In
technical terms, you have weakened the form’s encapsulation, its ability to hide
its internal details from the outside world.
In this case, you want to allow access to this label’s
Text property so marking
the label as public isn’t terribly unreasonable. However, by making the label
public you make all of its properties, methods, and events public, not just its
Text property.
A more restrictive approach would be to add a public
SetCaption method to the
ColorForm. Then other code would call that method instead of setting the label’s
text directly. You learn how to build methods such as this one in Lesson 20.
TRY IT
In this Try It, you create an application similar to the one shown in Figure 9-5. When the user
clicks the main forms buttons, the program displays the other forms non-modally.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson09 folder in the download.
Lesson Requirements
In this lesson, you:
Create the forms shown in Figure 9-5.
Declare the form variables outside of any event handler.
596906c09.indd 109 4/7/10 12:32:19 PM
110
LESSON 9 Creating and displaying new Forms
In the main form’s
Load event handler, add code to create the form instances but don’t dis-
play the forms.
Add code to the main form’s
Button event handlers to display the corresponding secondary
forms non-modally.
FIGURE 95
Hints
Normally every form appears in the taskbar. To avoid cluttering the taskbar with all of the
secondary forms, set their ShowInTaskbar properties to False.
Step-by-Step
Create the forms shown in Figure 9-5.
1. Create the main form.
a. Start a new project. In the Properties window, expand the main form’s Font
property and set its
Size sub-property to 12.
b. Add the Buttons. Center them as a group and set their Anchor properties
to
None.
2. Create the GettingThereForm.
a. Open the Project menu and select Add Windows Form. Enter the form type name
GettingThereForm and click Add.
596906c09.indd 110 4/7/10 12:32:20 PM
Try It
111
b. Set the form’s ShowInTaskbar property to False.
c. Add the Label, ListBox, and Buttons. Set the ListBox’s Anchor property to
Top, Bottom, Left. Set the ButtonsAnchor properties to Bottom, Right.
3. Create the GettingAroundForm.
a. Repeat step 2 for the GettingAroundForm.
4. Create the LodgingForm.
a. Repeat step 2 for the LodgingForm.
5. Create the FunStuffForm.
a. Repeat step 2 for the FunStuffForm. Leave the CheckBoxesAnchor properties
with their default values
Top, Left.
Declare the form variables outside of any event handler.
1. Add the following to the main form’s code module outside of any event handlers:
// The remote forms.
GettingThereForm theGettingThereForm;
GettingAroundForm theGettingAroundForm;
LodgingForm theLodgingForm;
FunStuffForm theFunStuffForm;
In the main form’s
Load event handler, add code to create the form instances but don’t dis-
play the forms.
1. Use code similar to the following:
// Initialize the forms but don’t display them.
private void Form1_Load(object sender, EventArgs e)
{
theGettingThereForm = new GettingThereForm();
theGettingAroundForm = new GettingAroundForm();
theLodgingForm = new LodgingForm();
theFunStuffForm = new FunStuffForm();
}
Add code to the main form’s
Button event handlers to display the corresponding secondary
forms non-modally.
1. Create the Button Click event handlers and make each call the corresponding form
variable’s
Show method:
// Display the getting there form.
private void gettingThereButton_Click(object sender, EventArgs e)
{
theGettingThereForm.Show();
}
// Display the getting around form.
private void gettingAroundButton_Click(object sender, EventArgs e)
{
596906c09.indd 111 4/7/10 12:32:20 PM
112
LESSON 9 Creating and displaying new Forms
theGettingAroundForm.Show();
}
// Display the lodging form.
private void lodgingButton_Click(object sender, EventArgs e)
{
theLodgingForm.Show();
}
// Display the fun stuff form.
private void funnStuffButton_Click(object sender, EventArgs e)
{
theFunStuffForm.Show();
}
Please select Lesson 9 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Make a program that displays a Button that says “New Form.” When the user clicks the
Button, display a new non-modal instance of the same kind of form. (What happens when
you click the new form’s button? What happens if you close the new form? What happens if
you make several forms and close the original one?)
2. Copy the program you made for Exercise 1 and add a TextBox named valueTextBox to the
form. Before you display the new form, copy the main form’s
TextBox value into the new
form’s
TextBox. (Hint: You don’t need to set the TextBox’s Modifiers property to Public
because the new form is the same kind as the old one. You need to do this only if a form of
one type wants to peek at the controls on a form of a different type.)
3. Make a program that displays a TextBox and a “New Form” Button. When the user clicks
the
Button, display a new form of type MessageForm modally.
The
MessageForm holds two Labels. The first Label says “You entered.” The second Label
is blank. When it displays the
MessageForm, the main program should copy whatever is
in its
TextBox into the MessageForms second label. (Hint: Now you need to set the labels
Modifiers property to Public.)
4. Build the PickAPicture program shown in Figure 9-6. When the user clicks one of the thumb-
nail images on the main form, the program displays a
PictureForm showing the image at full
scale. Use whatever images you like. (Hints: Display the thumbnail images in
PictureBoxes
with
ScaleMode set to Zoom. Set the PictureForm’s BackgroundImage property equal to the
PictureBox’s Image value.)
5. Extra Credit: As I’ve mentioned before, redundant code is usually a sign that the pro-
gram’s structure can be improved. The PickAPicture program from Exercise 4 uses four
practically identical event handlers. The only difference is the image that they assign to the
PictureForm’s background.
596906c09.indd 112 4/7/10 12:32:20 PM
..................Content has been hidden....................

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