52
LESSON 4 Handling EvEnts
EVENT MEANING
MouseEnter
The mouse has entered the control.
MouseHover
The mouse has hovered over the control.
MouseLeave
The mouse has left the control.
MouseMove
The mouse has moved while over the control.
MouseUp
The user released a mouse button over the control.
Move
The control has moved.
Paint
The control needs to be redrawn. (This is useful for drawing graphics.)
Resize
The control has resized.
Scroll
The slider on a TrackBar or scrollbar was moved by the user.
SelectedIndexChanged
A ComboBox’s or ListBox’s selection has changed.
TextChanged
The control’s Text property has changed. (This is particularly useful
for
TextBoxes.)
Tick
A Timer control’s Interval has elapsed.
ValueChanged
The value of a TrackBar or scrollbar has changed (whether by the
user or by code).
TRY IT
In this Try It, you use event handlers to display color samples as the user adjusts red, green, and
blue scrollbars.
Figure 4-4 shows the finished program in action. When
you change a scrollbar’s value, the label to the right shows
the new value and the large label on the far right shows a
sample of the color with the selected red, green, and blue
color components.
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 Lesson04 folder in the download.
FIGURE 44
TABLE 41
(continued)
596906c04.indd 52 4/7/10 12:31:46 PM
Try It
53
Lesson Requirements
In this lesson, you:
Create the form shown in Figure 4-4. Arrange the controls and set their
Anchor properties.
Make an event handler for the red scrollbar that displays all three color values and the
color sample.
Attach the event handler to the green and blue scrollbars, as well as the red one.
Hints
This Try It requires a few techniques that haven’t been covered yet, but it’s not too hard to build
with a couple of hints.
A scrollbar’s
Value property is an integer. To convert it into a string so you can display it in
the labels, call its
ToString method. For example, the following code makes the redLabel
control display the
redHScrollBar’s Value property:
redLabel.Text = redHScrollBar.Value.ToString();
The
Color class’s FromArgb method returns a color with given red, green, and blue color
components between 0 and 255. For example,
Color.FromArgb(255, 128, 0) returns the
color orange (red = 255, green = 128, and blue = 0). Pass this method the values selected by
the scrollbars (returned by their
Value properties) and assign the result to the sample label’s
BackColor property.
Step-by-Step
Create the form shown in Figure 4-4. Arrange the controls and set their
Anchor properties.
1. Create the controls as shown in Figure 4-4. For the scrollbars, set Minimum = 0, Maximum
= 264, SmallChange = 1, LargeChange = 10, and Anchor = Top, Left, Right.
For some bizarre reason, the largest value that a user can select with a scrollbar
is
Maximum - LargeChange + 1. If Maximum = 264 and LargeChange = 10, the
largest selectable value is 264 - 10 + 1 = 255, so these properties let the user
select values between 0 and 255.
Make an event handler for the red scrollbar that displays all three color values and the color
sample.
1. Double-click the red scrollbar to create an empty event handler for the control’s Scroll
event. Type the bold lines in the following code so the event handler looks like this:
private void hbarRed_Scroll(object sender, ScrollEventArgs e)
{
lblRed.Text = hscrRed.Value.ToString();
596906c04.indd 53 4/7/10 12:31:47 PM
54
LESSON 4 Handling EvEnts
lblGreen.Text = hscrGreen.Value.ToString();
lblBlue.Text = hscrBlue.Value.ToString();
lblSample.BackColor =
Color.FromArgb(hscrRed.Value, hscrGreen.Value, hscrBlue.Value);
}
Attach the event handler to the green and blue scrollbars, as well as the red one.
1. In the Form Designer, click the green scrollbar. In the Properties window, click the
event button (the lightning bolt). Then click the control’s
Scroll event, click the drop-
down arrow to the right, and select the event handler.
2. Repeat the previous steps for the blue scrollbar.
Run the program and experiment with it. Note how the largest value you can select in the
scrollbars is 255.
Please select Lesson 4 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Build the DynamicEvents example program shown in Figure 4-3. What happens if you click
Attach twice? Three times? What happens if you then click Detach once? Five times?
2. Create a form with one Button labeled “Stop” and two Timers. Set the TimersInterval
properties to 1000. At design time, set the first
Timer’s Enabled property to True.
In each
Timer’s Tick event handler, disable that Timer and enable the other one.
Make one
Timer’s Tick event handler also move the Button to (10, 10) by setting
its
Left and Top properties.
Make the other
Timer’s Tick event handler move the Button to (200, 200).
In the
Button’s Click event handler, set Enabled = false for both Timers.
Run the program. Experiment with different values for the
TimersInterval properties.
What happens if
Interval = 10?
3. Make a program similar to the one shown in
Figure 4-5. When the user changes the scrollbar
values, the program should set the
PictureBox’s
Left and Top properties. Use Anchor properties
to keep the scrollbars at the form’s edges and
make the background
Panel fill most of the form.
(Hint: When the form loads and when the
Panel
resizes, set the scrollbars’
Maximum properties so
they match the
Panel’s size. You can use the
same event handler for both.)
FIGURE 45
596906c04.indd 54 4/7/10 12:31:47 PM
Exercises
55
4. Make a program similar to the one shown in Figure 4-6.
Use
Anchor properties to make the buttons stick to the form’s lower-right corner.
Make the
Buttons be the form’s accept and cancel buttons, and make them display
messages saying “OK” or “Canceled.”
Make the
CheckBoxesCheckedChanged events enable or disable the corresponding
GroupBoxes.
Hint: If you drag a
CheckBox onto a GroupBox, it falls into the GroupBox. (Try it and
run the program to see why thats bad.) To prevent this, position the
CheckBoxesrst
and then position the
GroupBoxes on top of them. Right-click a GroupBox and select
Send to Back if you need to move it behind a
CheckBox.
Hint: The
GroupBoxes have Text set to a blank value. The CheckBoxes label
the groups.
Hint: Set a
GroupBox’s Enabled property equal to the corresponding CheckBox’s
CheckedChanged value.
FIGURE 46
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 Lesson04 folder of the download.
596906c04.indd 55 4/7/10 12:31:48 PM
Click here to Play
596906c04.indd 56 4/7/10 12:31:48 PM
..................Content has been hidden....................

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