MouseDown和计时器的组合(combination of MouseDown and timer)

编程入门 行业动态 更新时间:2024-10-24 22:30:33
MouseDown和计时器的组合(combination of MouseDown and timer)

我尝试加载一个像“holdFunction”这样的函数,在触摸(MouseDown)期间以及最大1秒期间。

因此,当用户尝试触摸并保持一秒钟时,我必须调用该函数,而这与mouseUp无关。

也许我必须把这些结合起来:

private DateTime dtHold; private void EditProduct_MouseDown(object sender, MouseButtonEventArgs e) { dtHold = DateTime.Now; } private void EditProduct_MouseUp(object sender, MouseButtonEventArgs e) { TimeSpan interval = TimeSpan.FromSeconds(1); if (DateTime.Now.Subtract(dtHold) > interval) { //HoldFunction(); } }

System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); private void EditProduct_MouseDown(object sender, MouseButtonEventArgs e) { dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0, 0, 0,1,0); dispatcherTimer.Start(); } private int _sec = 0; private void dispatcherTimer_Tick(object sender, EventArgs e) { _sec = _sec + 1; if (_sec == 2) { dispatcherTimer.Stop(); { //HoldFunction(); } _sec = 0; return; } }

I try to load a function like "holdFunction", during touching(MouseDown) and during at max 1 second .

So when user try to touch and hold for a second I have to call the function, and this isn't related to mouseUp.

Maybe I must to combine these:

private DateTime dtHold; private void EditProduct_MouseDown(object sender, MouseButtonEventArgs e) { dtHold = DateTime.Now; } private void EditProduct_MouseUp(object sender, MouseButtonEventArgs e) { TimeSpan interval = TimeSpan.FromSeconds(1); if (DateTime.Now.Subtract(dtHold) > interval) { //HoldFunction(); } }

and

System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); private void EditProduct_MouseDown(object sender, MouseButtonEventArgs e) { dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0, 0, 0,1,0); dispatcherTimer.Start(); } private int _sec = 0; private void dispatcherTimer_Tick(object sender, EventArgs e) { _sec = _sec + 1; if (_sec == 2) { dispatcherTimer.Stop(); { //HoldFunction(); } _sec = 0; return; } }

最满意答案

这是你想要的?

如果用户持有MouseDown 1秒,evnt被解雇?

public partial class Window2 : Window { private DispatcherTimer _DispatcherTimer = new DispatcherTimer(); public Window2() { InitializeComponent(); MouseDown += _MouseDown; MouseUp += _MouseUp; _DispatcherTimer.Interval = TimeSpan.FromSeconds(1.0); _DispatcherTimer.Tick += _DispatcherTimer_Tick; } private void _DispatcherTimer_Tick(object sender, EventArgs e) { _DispatcherTimer.Stop(); Title = DateTime.Now.ToString(); } private void _MouseUp(object sender, MouseButtonEventArgs e) { _DispatcherTimer.Stop(); } private void _MouseDown(object sender, MouseButtonEventArgs e) { _DispatcherTimer.Start(); } }

Is this what you are looking for?

If the User holds the MouseDown for 1 second, the evnt is fired?

public partial class Window2 : Window { private DispatcherTimer _DispatcherTimer = new DispatcherTimer(); public Window2() { InitializeComponent(); MouseDown += _MouseDown; MouseUp += _MouseUp; _DispatcherTimer.Interval = TimeSpan.FromSeconds(1.0); _DispatcherTimer.Tick += _DispatcherTimer_Tick; } private void _DispatcherTimer_Tick(object sender, EventArgs e) { _DispatcherTimer.Stop(); Title = DateTime.Now.ToString(); } private void _MouseUp(object sender, MouseButtonEventArgs e) { _DispatcherTimer.Stop(); } private void _MouseDown(object sender, MouseButtonEventArgs e) { _DispatcherTimer.Start(); } }

更多推荐

private,dispatcherTimer,DateTime,dtHold,电脑培训,计算机培训,IT培训"/> <meta

本文发布于:2023-04-27 23:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   计时器   MouseDown   combination   timer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!