0

I am trying to detect if the user did not click directly on the slider but somewhere on the track bar where it makes the slider value jump by LargeChange property. I am trying to get the value the slider was on before the jump occurred. So for example, if the slider is on the value 40 and the user makes it jump to 140, I need to know the initial value before it was changed.

I know I can use variables to keep track of the previous values, but I have a lot of TrackBars so I will end up having a lot of variables which I am trying to avoid.

Is there some kind of event for the TrackBar I can use to detect the jump or get the initial value before it has been changed by the user? Right now, I'm using the MouseDown event, but that gives me the value of wherever I click on the TrackBar instead of where it was at.

Talking about this TrackBar (the slider being the thing you can move left and right):

enter image description here

syy
  • 687
  • 2
  • 13
  • 32

2 Answers2

3

A solution that jumps to mind is to use a dictionary. You don't want to have many variables, but a dictionary is only one variable. And I don't believe you having so many trackbars that the memory for the dictionary should be any problem.

So declare a member in your Form class like

private readonly Dictionary<TrackBar, int> trackBarValue = new Dictionary<TrackBar, int>();

And in the handler for the ValueChanged event of the TrackBars you can do

private void TrackBarsValueChanged(object sender, EventArgs e)
{
    TrackBar clickedBar = sender as TrackBar;
    if (clickedBar == null) return;

    if (!trackBarValues.ContainsKey(clickedBar))
        trackBarValues[clickedBar] = clickedBar.Value;

    if (Math.Abs(trackBarValues[clickedBar] - clickedBar.Value) > clickedBar.LargeChange)
    {
        // yes, it was a big change
    }

    // store the current value
    trackBarValues[clickedBar] = clickedBar.Value;

}

I don't know if you currently have a different handler for each single trackbar. Since that would mean to repeat that code in every handler, it is possibly a good idea to add this single handler to the events of all trackbars.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
1

See here for how to execute code when a variable changes.

Try something like that, but have the code set something like OldVar equal to TrackVar2, TrackVar2 being the value of TrackBar1 (your actual trackbar int) but only update it after OldVar has been updated first.

This should give you what you want, there is likely a way better solution but this should work.

Community
  • 1
  • 1
Lauren Hinch
  • 340
  • 1
  • 11