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 Timers’ Interval
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
Timers’ Interval 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 45
596906c04.indd 54 4/7/10 12:31:47 PM