I am using mousehover for click in c# but I want it to wait 2-3 sec before click and if cursor does not stay on a button for 2 or more seconds, it won't click.
Asked
Active
Viewed 3,446 times
1
-
4What technology are you using? WinForms, WPF or some other XAML platform, something else? (Hopefully not HTML/CSS, you can't use C# there) – Kris Vandermotten Jun 05 '12 at 07:02
-
4use a timer. BTW "provide me the code" is **very** unwelcome behavior here. – Alex Jun 05 '12 at 08:34
-
If i use timer here, it will just give a delay.And if i remove the pointer in less than 3sec, it will just click it with a delay – user1436685 Jun 05 '12 at 08:53
1 Answers
2
You could implement that functionality by deriving from Button
class:
using System;
using System.Windows.Forms;
namespace MouseHoverDelay
{
public class HoverButton : Button
{
protected System.Timers.Timer timer;
public bool IsHoverEnabled { get; set; }
public double Delay { get; set; }
public event System.Timers.ElapsedEventHandler TimerElapsed
{
add
{
timer.Elapsed += value;
}
remove
{
timer.Elapsed -= value;
}
}
public HoverButton()
{
// defaults: hover trigger enabled with 3000 ms delay
IsHoverEnabled = true;
Delay = 3000;
timer = new System.Timers.Timer
{
AutoReset = false,
Interval = Delay
};
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
if (IsHoverEnabled)
{
timer.Start();
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
timer.Stop();
}
}
}
After adding it to the form you need to set it's Delay
and handler method inside the InitializeComponent()
method of the form:
this.btnHoverTest.Delay = 2000;
this.btnHoverTest.TimerElapsed += timer_Elapsed;
And then implement the handler in the form:
using System;
using System.Windows.Forms;
namespace MouseHoverDelay
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// triggers when Delay milliseconds have passed since hovering mouse over control
protected void timer_Elapsed(object o, EventArgs e)
{
MessageBox.Show("Hovered for 2 seconds!");
}
}
}

Răzvan Flavius Panda
- 21,730
- 17
- 111
- 169