The Testing Department is calling again—the two scroll bars you've added to your SuperDuperWinBigCasino game look great, but there's one problem. A pair of scroll bars straddle the user's view of the roulette table in SuperDuperWinBigCasino, but when you scroll one, the other doesn't move to match it. Can you fix that?
It's common to have two scroll bars that perform the same scrolling action—one on either side of an image you're scrolling, for example. The user should be able to scroll either scroll bar and have the other one match.
It's easy to keep scroll bars coordinated. All you have to do is to make sure that when one scroll bar has a Scroll event, you update the other scroll bar's Value property. For example, if we have two vertical scroll bars, VScrollBar1 and VScrollBar2, that straddle an object they're in charge of scrolling, you can up-date VScrollBar2 when VScrollBar1 changes, this way:
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _ VScrollBar1.Scroll VScrollBar2.Value = e.NewValue End Sub Private Sub VScrollBar2_Scroll(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _ VScrollBar2.Scroll VScrollBar1.Value = e.NewValue End Sub
That's all there is to it—now the scroll bars are coordinated.
Tip |
If appropriate for your code, another way of doing this is to give each scroll bar the same Scroll event handler. To see an example of this, take a look at "Scrolling Images" in this chapter. |