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

后端 未结 7 1257
鱼传尺愫
鱼传尺愫 2021-01-05 10:18

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 mi

相关标签:
7条回答
  • 2021-01-05 10:50

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

    0 讨论(0)
  • 2021-01-05 11:01

    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.

    0 讨论(0)
  • 2021-01-05 11:04

    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-05 11:05

    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.

    0 讨论(0)
  • 2021-01-05 11:05

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-05 11:07

    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;
        }
    
    0 讨论(0)
提交回复
热议问题