You've added the scroll bars the Testing Department wanted. You've set the scroll bars' Minimum, Maximum, SmallChange, and LargeChange properties. Now what-how do you respond to the scroll bars in your program's code?
There are two events you typically use in scroll bars: Scroll-which happens when the scroll box is moved either with the mouse or keyboard, and ValueChanged-which happens when the Value property changes, even by one unit, whether through the user's actions, or in code. In a normal scroll operation, many ValueChanged events occur-as, for example, the Value property changes from 42 to 43, then to 44, then to 45, and so on.
I usually use the Scroll event. When the user changes the setting in a scroll bar, a Scroll event occurs; you can react to those changes with an event handler attached to that event. For example, you may use scroll bars to move other controls around on the form (using those controls' SetBounds method), and when the user changes a scroll bar's setting, you'll be informed of the new value in the Scroll event handler.
Let's take a look at an example that does exactly that. In this case, I'll use two scroll bars docked to the top and right side of a form to move a label control around the form; this example is called ScrollBars on the CD-ROM.
I start by adding two scroll bars-a horizontal scroll bar, HScrollBar1, and a vertical scroll bar, VScrollBar1-to a form. Dock those two scroll bars to the edges of the form using their Dock properties, as you see in Figure 8.1. Next, add a label, Label1, to the form; this is the label we'll move around with the scroll bars.
When a scroll bar is scrolled, it triggers a Scroll event. You can determine the new setting of the scroll bar with its Value property. However, there's more information available to you here in the ScrollEventArgs object passed to you in the Scroll event handler. This object has two members, NewValue, which gives you the new setting of the scroll bar, and Type, which tells you the type of the scroll operation. The Type values come from the ScrollEventType enumeration:
EndScroll- The scroll box has stopped moving.
First- The scroll box was moved to the Minimum position.
LargeDecrement- The user clicked the scroll bar to the left (horizontal scroll bars) or above (vertical scroll bars) the scroll box, or pressed the Page Up key.
LargeIncrement- The user clicked the scroll bar to the right (horizontal scroll bars) or below (vertical scroll bars) the scroll box, or pressed the Page Down key.
Last- The scroll box was moved to the Maximum position.
SmallDecrement- The user clicked the left (horizontal scroll bars) or top (vertical scroll bars) scroll arrow or pressed the Up Arrow key.
SmallIncrement- The user clicked the right (horizontal scroll bars) or bottom (vertical scroll bars) scroll arrow or pressed the Down Arrow key.
ThumbPosition- The scroll box was moved.
ThumbTrack- The scroll box is currently being moved.
We can move the label around using the scroll bars in our example if we know the present dimensions of the form, which we can get as Me.Size.Width and Me.Size.Height. Because, by default, the values returned by our scroll bars can go up to a maximum of 100, we can move the label like this, setting its text to "I'm moving!":
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _ HScrollBar1.Scroll Label1.Location = New Point(e.NewValue * Me.Size.Width / 100, _ Label1.Location.Y) Label1.Text = "I'm moving!" End Sub Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _ VScrollBar1.Scroll Label1.Location = New Point(Label1.Location.X, _ e.NewValue * Me.Size.Height / 100) Label1.Text = "I'm moving!" End Sub
There's also another scroll bar in this example-a horizontal one-that simply displays its current setting in a text box. Here's the code for that scroll bar:
You can see the results in Figure 8.1-when you use the docked scroll bars at the edge of the form, the label moves to match. And you can use the undocked horizontal scroll bar to increment or decrement the value shown in the text box. That's all it takes-now we're handling scroll bar events.