Track Bar Only fire event on final value not ever time value changes

我怕爱的太早我们不能终老 提交于 2019-11-30 20:42:28

Just check a variable, if the user clicked the track bar. If so, delay the output.

bool clicked = false;
trackBar1.Scroll += (s,
                        e) =>
{
    if (clicked)
        return;
    Console.WriteLine(trackBar1.Value);
};
trackBar1.MouseDown += (s,
                        e) =>
{
    clicked = true;
};
trackBar1.MouseUp += (s,
                        e) =>
{
    if (!clicked)
        return;

    clicked = false;
    Console.WriteLine(trackBar1.Value);
};

For the problem @roken mentioned, you can set LargeChange and SmallChange to 0.

Asher P

Try the MouseCaptureChanged event - that is the best for this task

A user could also move the track bar multiple times in a short period of time, or click on the track multiple times to increment the thumb over instead of dragging the thumb. All being additional cases where the value that registers at the end of a "thumb move" is not really the final value your user desires.

Sounds like you need a button to confirm the change, which would then capture the current value of the trackbar and send it off to your devices.

I found a fairly reliable way to do this is to use a timer hooked up in the trackbar.Scroll event:

private Timer _scrollingTimer = null;

private void trackbar_Scroll(object sender, EventArgs e)
{
    if (_scrollingTimer == null)
    {
        // Will tick every 500ms (change as required)
        _scrollingTimer = new Timer() 
        {
                Enabled = false,
                Interval = 500,
                Tag = (sender as TrackBar).Value
        };
        _scrollingTimer.Tick += (s, ea) =>
        {
            // check to see if the value has changed since we last ticked
            if (trackBar.Value == (int)_scrollingTimer.Tag)
            {
                // scrolling has stopped so we are good to go ahead and do stuff
                _scrollingTimer.Stop();

                // Do Stuff Here . . .

                _scrollingTimer.Dispose();
                _scrollingTimer = null;
            }
            else
            {
                // record the last value seen
                _scrollingTimer.Tag = trackBar.Value;
            }
        };
        _scrollingTimer.Start();
    }
}
user1061964

Try this with the trackbar_valuechanged event handler:

trackbar_valuechanged(s,e) {
    if(trackbar.value == 10){
        //Do whatever you want
    } else{
        //Do nothing or something else
    }
}

I had this problem just now as I'm implementing a built in video player and would like the user to be able to change the position of the video but I didn't want to overload the video playback API by sending it SetPosition calls for every tick the user passed on the way to his/her final destination.

This is my solution:

First, the arrow keys are a problem. You can try your best to handle the arrow keys via a timer or some other mechanism but I found it more pain than it is worth. So set the property SmallChange and LargeChange to 0 as @Matthias mentioned.

For mouse input, the user is going to have to click down, move it, and let go so handle the MouseDown, MouseUp, and the Scroll events of the trackbar like so:

    private bool trackbarMouseDown = false;
    private bool trackbarScrolling = false;

    private void trackbarCurrentPosition_Scroll(object sender, EventArgs e)
    {
        trackbarScrolling = true;
    }

    private void trackbarCurrentPosition_MouseUp(object sender, MouseEventArgs e)
    {
        if (trackbarMouseDown == true && trackbarScrolling == true)
            Playback.SetPosition(trackbarCurrentPosition.Value);
        trackbarMouseDown = false;
        trackbarScrolling = false;
    }

    private void trackbarCurrentPosition_MouseDown(object sender, MouseEventArgs e)
    {
        trackbarMouseDown = true;
    }

I had a similar problem, only with a range TrackBar Control. Same idea applies to this also, only it's easier for this case.

I handled the MouseUp Event on the TrackBar to launch the procedures I needed, only after you would let go of the mouse button. This works if you drag the bar to your desired position or just click it.

private void rangeTrackBarControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { YourProcedureHere(); }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!