c# - 阻止滚动条的值更改事件处理程序,直到该条被释放

标签 c# .net winforms scrollbar

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

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
        {

        }
    }
}

但是,它不起作用。

我该怎么做?

最佳答案

你可以处理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
    }
}

关于c# - 阻止滚动条的值更改事件处理程序,直到该条被释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52227636/

相关文章:

c# - 来自服务器的响应的 .NET header

c# - 取消保存时引发 DocumentBeforeSaveEventHandler 事件

c# - 如何将包含 ILIST 成员的对象序列化为 XML?

c# - 读取 XML 的多个部分

c# - 有没有办法在应用程序中显示数据集可视化工具?

c# - 获取呈现的 html 最终源

c# - Winforms 文本框的焦点丢失事件?

c# - 发票没有 LAN 自动化? [加1]

c# - 内存流到 sbyte[]

c# - 如何使用 C# 和 ASP.net 代理 WebRequest?