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

后端 未结 7 1336
鱼传尺愫
鱼传尺愫 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 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;
        }
    

提交回复
热议问题