Track bar Only event with fire at a final value that does not change the value of time

I am working on a fairly simple Visual Studio Forms C # application, but I have a problem in getting the track bar to act the way I want it to be so hopeful that someone in the community can have a solution for this.

I have a pretty basic application, the main part of which is a strip of tracks with a value from 0 to 100. The user sets the value of the track to represent the "amount of work to do", after which the program calls on some devices and tells them to do "x" amount of work (x is the value of the trackbar). So I use the track scroll event to catch when the column value of the track has changed, and inside the handler is called on the device and tells them how much to work.

My problem is that my event handler is called for each value between where the track bar is currently located and where it ends. Therefore, if it is shifted from 10 to 30, my event handler is called 20 times, which means that I access my devices and tell them to start with values ​​that I don’t even want them to start. Is there ever an event when scrolling has stopped, so you can check the final value?

+7
source share
7 answers

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

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 @roken problem mentioned, you can set LargeChange and SmallChange to 0.

+5
source

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

+2
source

The user can also move the strip of tracks several times in a short period of time or press the track several times to increase the thumb of the thumb instead of dragging the thumb. These are all additional cases where the value that is recorded at the end of the “thumb move” is not really the final value that your user desires.

It looks like you need a button to confirm the change, which then captures the current value of the trackbar and sends it to your devices.

+1
source

I found a pretty reliable way to do this - use a timer connected in the trackbar. Scrolling 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(); } } 
+1
source

Try this with the trackbar_valuechanged event trackbar_valuechanged :

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

I had this problem only now when I implement the built-in video player and I want the user to be able to change the position of the video, but I did not want to overload the video playback API by sending his SetPosition calls to every tick that the user went along the way to his final destination .

This is my decision:

First, the arrow keys are a problem. You can do your best to process the arrow keys through a timer or some other mechanism, but I found more pain than it costs. Therefore, set the SmallChange and LargeChange property to 0, as indicated in @Matthias.

To enter the mouse, the user will have to click, move and release it so that it handles the MouseDown, MouseUp and Scroll events for the trackpad as follows:

  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
source

I had a similar problem, only with the TrackBar Control range. The same idea applies to this, only for this case it is easier.

I handled the MouseUp event on the TrackBar to start the procedures I needed, only after you release the mouse button. This works if you drag the panel to the desired position or simply click on it.

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

0
source

All Articles