34
LESSON 2 Creating Controls
2. Type the bold text in the following code so the event handler looks like this:
// Create the new customer.
private void okButton_Click(object sender, EventArgs e)
{
resultLabel.Text = “New User Created”;
resultLabel.BackColor = Color.LightGreen;
resultLabel.Visible = true;
}
Add code behind the Cancel button to display the result label with a hot pink background
and the text “Operation Canceled.”
1. Double-click the Cancel button to create an event handler for its Click event.
2. Type the bold text in the following code so the event handler looks like this:
// Don’t create the new customer.
private void cancelButton_Click(object sender, EventArgs e)
{
resultLabel.Text = “Operation Canceled”;
resultLabel.BackColor = Color.HotPink;
resultLabel.Visible = true;
}
Now run the code and experiment with the program. Notice what happens when you press the [Enter]
and [Esc] keys while focus is in a
TextBox. See what happens if focus is on one of the buttons.
Please select Lesson 2 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Build a checkerboard similar to the one
shown in Figure 2-10. (Hints: The squares are
PictureBoxes with different background colors.
Give the form a bluish background. Finally,
use the Format menu or Layout toolbar to align
the controls.)
2. Make a tic-tac-toe (or naughts-and-crosses)
board similar to the one shown in Figure 2-11.
(Hints: Make three labels for each square, named
after the rows and columns. For the upper-left
square, name them
x00Label for the little
X label,
o00Label for the little Y label, and
taken00Label for the big label. Give the smaller
labels
Click event handlers that set the Text
FIGURE 210
596906c02.indd 34 4/7/10 12:31:35 PM
Exercises
35
property of the big label appropriately. Don’t worry about the rules such as not allowing some-
one to claim a square that is already claimed.)
3. Modify the tic-tac-toe program from Exercise 2 so instead of
displaying X or O in each square, it displays a picture. Use
your favorite football team logos, a cat and a dog, your pic-
ture and your boss’s, or whatever. (Hints: Use
PictureBoxes
instead of the large
Labels. Add two hidden PictureBoxes
to the form. To set their
Image properties, click the ellipsis
next to the
Image property in the Properties window, click the
Import button, and browse for the image files. Finally, instead
of setting a
Label’s Text property, the Click event handlers
should set the appropriate
PictureBox’s Image property
equal to one of the hidden
PictureBox’s Image properties.
Set all
PictureBoxesSizeMode properties to Zoom.)
4. Make a program with a Label that says “Move Me” and four Buttons with text (0, 0),
(200, 200), (200, 0), and (0, 200). Make each
Button move the Label to the corresponding
position by setting its
Left and Top properties.
5. The solution to Exercise 4 moves its Label in two steps by setting its Left and Top properties.
Modify the program so it sets the
Label’s Location property in a single step using code similar
to this:
moveMeLabel.Location = new Point(0, 0);
6. Build a menu selection form similar to the one
shown in Figure 2-12. (Hints: Copy and paste
the
Labels and TextBoxes from the Try It pro-
gram. To set the
PictureBox’s image, look in the
Properties window and click the ellipsis next to
the
Image property. In the Select Resource dialog
box, click Import and browse to select a picture.
Finally, set the
PictureBox’s SizeMode property
to
AutoSize.)
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find them in
the Lesson02 folder.
FIGURE 211
FIGURE 212
596906c02.indd 35 4/7/10 12:31:36 PM
Click here to Play
596906c02.indd 36 4/7/10 12:31:36 PM
..................Content has been hidden....................

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