阻止滚动条的值更改事件处理程序,直到释放栏

问题描述 投票:3回答:1

假设,我需要通过滚动条的移动执行资源密集型任务。

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
    {

    }
}

我面临的问题是,事件处理程序会在滚动条稍有变化的情况下执行。

我需要阻止事件处理程序的执行,直到释放鼠标。

所以,我尝试使用鼠标输入和鼠标离开事件处理程序,如:

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

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

以及像这样的检查:

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
        {

        }
    }
}

但是,它不起作用。

我怎样才能做到这一点?

c# .net winforms scrollbar
1个回答
2
投票

你可以处理Scroll事件并检查e.Type,如果它是ScrollEventType.EndScroll,运行你的逻辑:

private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
    if (e.Type == ScrollEventType.EndScroll)
    {
        // Scroll has ended
        // You can use hScrollBar1.Value
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.