# Visual Basic > Visual Basic .NET >  Trackbar synchronization

## Andrew9

Hello everyone,
can someone help me with a code to synchronize, real time, two trackbars, each set with a min/max. different?
For example: 

1st trackbar: min.0 - max.10000
2nd trackbar: min.0-max-5000

I would move the second slider (let's say for an amount equal to 2000) and at the same time, the first should decrease by the same amount. Conversely when I move the slider down, the first trackbar should increase again, by the same amount. If I connect the two trackbars with =.value, I set the same value or, if I set a simple subtraction between the two trackbar values, the first is always increased or decreased by the "final" value assumed by the second and not step by step real time.

Thanks in advance

Andrew

----------


## OptionBase1

I'm not entirely sure if your last few sentences are explaining how you want it to work, or how your first attempt works now and you aren't happy with it, but something like this will synchronize "movement" between two trackbars.  If one has a value of 0, and the other has a value of 200, and you move the second one from 200 to 250, it will move the first one from 0 to 50.  It synchronizes the delta changes, not the individual values of each trackbar.

The code is kind of rough and I'm sure could be improved on, but it should give you an idea of how it might work:



```
Public Class frmSliderSync

    Dim tb1LastValue As Integer
    Dim tb2LastValue As Integer

    Private Sub frmSliderSync_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        tb1LastValue = TrackBar1.Value
        tb2LastValue = TrackBar2.Value
    End Sub

    Private Sub TrackBar1_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBar1.Scroll
        Dim tbDelta As Integer

        tbDelta = TrackBar1.Value - tb1LastValue
        If TrackBar2.Value + tbDelta >= TrackBar2.Minimum AndAlso TrackBar2.Value + tbDelta <= TrackBar2.Maximum Then
            TrackBar2.Value += tbDelta
        End If
        tb1LastValue = TrackBar1.Value
        tb2LastValue = TrackBar2.Value
    End Sub

    Private Sub TrackBar2_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBar2.Scroll
        Dim tbDelta As Integer

        tbDelta = TrackBar2.Value - tb2LastValue
        If TrackBar1.Value + tbDelta >= TrackBar1.Minimum AndAlso TrackBar1.Value + tbDelta <= TrackBar1.Maximum Then
            TrackBar1.Value += tbDelta
        End If
        tb1LastValue = TrackBar1.Value
        tb2LastValue = TrackBar2.Value
    End Sub

End Class
```

Edit:  Hmm...after re-reading your post, it sounds like you actually might want to move one slider "up", and then have the other slider move "down" by the same amount.  If that is the case, then the same code above can be used, you would just need to change the lines where tbDelta is calculated to be:



```
tbDelta = tb1LastValue - TrackBar1.Value
```

and 



```
tbDelta = tb2LastValue - TrackBar2.Value
```

Edited again:  I forgot to note that this code doesn't account for any changes that take place to the TrackBars outside of the scroll events.  So, if code somewhere else modifies TrackBar.value directly, then the tbxLastValue variables won't be properly updated using just the code above, which could cause the sliders jumping around unexpectedly when scrolling after such a change takes place.

----------


## Andrew9

Hi! Thank you for your reply. Actually... my bad.. I didn't explain very well sorry..  My aim,  as you stated, is to syncronize the "delta' change:

Trackbar1= 0
Trackbar2= 300

I move trackbar 2 from 300 to 350 (350-300=50)
Trackbar1 should move by 50

The same should happen when I move down trackbar2. For example:

Trackbar1= 3000
Trackbar2= 2000

I move trackbar2 down from 2000 to 1500 (2000-1500=500)
Trackbar1 should move down by 500

Thank you

----------


## OptionBase1

> Hi! Thank you for your reply. Actually... my bad.. I didn't explain very well sorry..  My aim,  as you stated, is to syncronize the "delta' change:
> 
> Trackbar1= 0
> Trackbar2= 300
> 
> I move trackbar 2 from 300 to 350 (350-300=50)
> Trackbar1 should move by 50
> 
> The same should happen when I move down trackbar2. For example:
> ...


Did you try the code I posted?  It does exactly that.

----------

