You can do it like this by using the Scroll
event. Both scrollbars need to handle the Scroll
event like so:
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
this.vScrollBar2.Scroll -= vScrollBar2_Scroll;
this.vScrollBar2.Value = (sender as ScrollBar).Value;
this.vScrollBar2.Scroll += vScrollBar2_Scroll;
}
private void vScrollBar2_Scroll(object sender, ScrollEventArgs e)
{
this.vScrollBar1.Scroll -= vScrollBar1_Scroll;
this.vScrollBar1.Value = (sender as ScrollBar).Value;
this.vScrollBar1.Scroll -= vScrollBar1_Scroll;
}
Please note when vScrollBar1
is scrolling, we will tell vScrollBar2
to scroll. Therefore, we do not want to trigger vScrollBar2.Scroll
since it will come back and tell vScrollBar1
to scroll at that instance (circular calls). Therefore we unsubscribe vScrollBar2.Scroll
event temporarily. After vScrollBar2
has scroll to the same position as vScrollBar1
, then we tell vScrollBar2
tp subscribe to its scroll event again. The same story with the other scrollbar.