[英]How to delay mouse hover time?
I'm making an application in C#. 我正在用C#编写应用程序。 Whenever I hover cursor on a button message should be displayed. 每当我将光标悬停在按钮上时,都应显示消息。 Also if I hover for about 3 seconds again a message should be displayed that 'Your mouse has been hovering for 3 seconds' on button. 另外,如果我再次将鼠标悬停了3秒钟,则应该在按钮上显示一条消息“鼠标已经悬停3秒钟”。
Try using this to solve the problem: 尝试使用此方法解决问题:
private void label1_MouseHover(object sender, EventArgs e)
{
label_Click(null, null); // this will fire click event
}
You have to set a timer and use MouseEnter/MouseLeave events as shown below: 您必须设置一个计时器并使用MouseEnter / MouseLeave事件,如下所示:
Timer t;
public MainWindow()
{
InitializeComponent();
t = new Timer(3000);
t.Elapsed += t_Elapsed;
}
void t_Elapsed(object sender, ElapsedEventArgs e)
{
MessageBox.Show("Your mouse has been hovering for 3 seconds");
}
private void btn_MouseEnter(object sender, MouseEventArgs e)
{
//MessageBox.Show("Hovered");
t.Start();
}
private void btn_MouseLeave(object sender, MouseEventArgs e)
{
t.Stop();
}
Xaml: Xaml:
<Button x:Name="btn" Content="Button" HorizontalAlignment="Left" MouseEnter="btn_MouseEnter" MouseLeave="btn_MouseLeave" Click="btn_Click"/>
Try using a System.Windows.Forms.Timer
object for this. 尝试为此使用System.Windows.Forms.Timer
对象。
For example, say you want your control to run a MessageBox
after the Cursor has reached three (3) seconds, this is what you can do: 例如,假设您希望控件在游标达到三(3)秒后运行MessageBox
,这是可以执行的操作:
[C#] [C#]
// Used to store the counting value.
private int _counter = 0;
private void control_MouseHover(object sender, EventArgs e)
{
// Create a new Timer object.
Timer timer = new Timer();
// Set the timer's interval.
timer.Interval = 1000;
// Create the Tick-listening event.
_timer.Tick += delegate(object sender, EventArgs e)
{
// Update the counter variable.
_counter++;
// If the set time has reached, then show a MessageBox.
if (_counter == 3) {
MessageBox.Show("Three seconds have passed!");
}
};
// Start the timer.
_timer.Start();
}
[VB.NET] [VB.NET]
Dim _counter As Integer
Private Sub Control_MouseHover(ByVal sender As Object, ByVal e As EventArgs) _
Handles Control.MouseHover
' Create a new Timer object.
Dim timer As New Timer()
' Set the timer's interval.
timer.Interval = 1000
' Create the Tick-listening event.
AddHandler _timer.Tick, Sub(sender As Object, e As EventArgs)
' Update the counter variable.
_counter += 1
' If the set time has reached, then show a MessageBox.
If _counter = 3 Then
MessageBox.Show("Three seconds have passed!")
End If
End Sub
' Start the timer.
_timer.Start()
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.