简体   繁体   English

在 C# 中滚动表格后几秒钟致电 function

[英]Call function few seconds after scroll form in C#

In form there is a timer and I want disable timer.tick when form scrolling and a few seconds after that again timer be enabled.在表单中有一个计时器,我想在表单滚动时禁用timer.tick并在几秒钟后再次启用计时器。 How it is possible?怎么可能? Best regards.最好的祝福。

You do not need to stop the timer from ticking.您不需要停止计时器的滴答声。 You just need to prevent it from doing anything if someone has scrolled recently.如果有人最近滚动,你只需要阻止它做任何事情。

Write a timestamp each time a scroll occurs.每次滚动发生时写一个时间戳。 You can do this by subscribing to the Scroll event.您可以通过订阅Scroll事件来做到这一点。

DateTime _lastScrollTime = DateTime.MinValue;

void MyControl_Scroll(object sender ScrollEventArgs e)
{
    _lastScrollTime = DateTime.Now;
}

Then in your timer event, check the timestamp to make sure nobody has scrolled recently before doing anything:然后在您的计时器事件中,检查时间戳以确保最近没有人在执行任何操作之前滚动:

void Timer_Tick(object sender, EventArgs e)
{
    if (_lastScrollTime > DateTime.Now.AddSeconds(-2)) return;

    //Do timer stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM