假设,我需要通过移动滚动条来执行资源密集型任务。
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/