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

99封情书 提交于 2019-11-30 04:02:06

问题


I am working on a pretty basic C# visual studio forms application but am having some issue getting the track bar to act as I want it to so hoping someone in the community might have a solution for this.

What I have is a pretty basic application with the main part being a track bar with a value of 0 to 100. The user sets the value of the track to represent "the amount of work to perform" at which point the program reaches out to some devices and tells them to do "x" amount of work (x being the value of the trackbar). So what I do is use the track bars scroll event to catch when the track bars value has changed and inside the handler call out to the devices and tells them how much work to do.

My issue is that my event handler is called for each value between where the track bar currently resides and where ever it ends. So if it is slid from 10 to 30, my event handler is called 20 times which means I am reaching out to my devices and telling them to run at values I don't even want them to run at. Is there someway only to event when scroll has stopped happening so you can check the final value?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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();
    }
}



回答5:


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
    }
}



回答6:


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;
    }



回答7:


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(); }



来源:https://stackoverflow.com/questions/9220691/track-bar-only-fire-event-on-final-value-not-ever-time-value-changes

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