How to check for TrackBar sliding with mouse hold and release

Saeid Yazdani picture Saeid Yazdani · Apr 19, 2011 · Viewed 9k times · Source

I have a trackbar in my WinForms program which by moving it a huge and time consuming method will be refreshed. Have a look at this code:

trackBar_ValueChanged(object sender, EventArgs e)
{
     this.RefreshData();
}

This track bar has 20 steps. If user grab the trackbar's slider and pull it from 20 to 0 in my case, 'RefreshData' will be executed 20 times although it shows correct data when it ends the process that is 'RefreshData' with value 0, I want to do something like it only calls the 'RefreshData' when the trackBar slider has been released so it wont process all the steps to the releasing point on the track bar.

Any help and tips to achieve this would be appericiated! Thanks.

Answer

Volkan Sen picture Volkan Sen · Jul 26, 2012

How About:

Boolean user_done_updating = false;

private void MytrackBar_ValueChanged(object sender, EventArgs e)
{
    user_done_updating = true;
}

private void MytrackBar_MouseUp(object sender, MouseEventArgs e)
{
    if (user_done_updating)
    {
        user_done_updating = false;
        //Do Stuff
    }
}