Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking the scroll-bar's value change event-handler until the bar is released

Suppose, I need to perform a resource intensive task with the movement of scroll-bar.

private void hScrollBar_ValueChanged(object sender, EventArgs e)
{
    ReCalculate();
}

void ReCalculate()
{
    try
    {
        int n = hScrollBar1.Value;
        int f0 = hScrollBar2.Value;
        int theta = hScrollBar3.Value;
        int a = hScrollBar4.Value;

        //... resource-intensive task which uses scroll-bar's values.
    }
    catch
    {

    }
}

The problem I am facing is, the event-handler gets executed with the slightest change of the scroll-bar.

I need to block the execution of event-handler until the mouse is released.

So, I tried using mouse-enter and mouse-leave event-handlers like:

bool ready = false;
private void hScrollBars_MouseEnter(object sender, EventArgs e)
{
      ready = false;
}

private void hScrollBars_MouseLeave(object sender, EventArgs e)
{
      ready = true;
}

along with a checking like:

void ReCalculate()
{
    if(ready)
    {
        try
        {
            int n = hScrollBar1.Value;
            int f0 = hScrollBar2.Value;
            int theta = hScrollBar3.Value;
            int a = hScrollBar4.Value;

            //... resource-intensive task which uses scroll-bar's values.
        }
        catch
        {

        }
    }
}

But, it doesn't work.

How can I do that?

like image 644
user366312 Avatar asked Dec 17 '25 15:12

user366312


1 Answers

You can handle Scroll event and check the e.Type and if it was ScrollEventType.EndScroll, run your logic:

private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
    if (e.Type == ScrollEventType.EndScroll)
    {
        // Scroll has ended
        // You can use hScrollBar1.Value
    }
}
like image 65
Reza Aghaei Avatar answered Dec 20 '25 08:12

Reza Aghaei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!