代码之家  ›  专栏  ›  技术社区  ›  user366312

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

  •  3
  • user366312  · 技术社区  · 7 年前

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

    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
            {
    
            }
        }
    }
    

    我该怎么做?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Reza Aghaei    7 年前

    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
        }
    }
    
    推荐文章