如果鼠标悬停3秒钟,请单击按钮 [英] Click button if mouse hovers for 3 seconds

查看:61
本文介绍了如果鼠标悬停3秒钟,请单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#winForms代码,当鼠标悬停在按钮上时会立即触发按钮的点击事件..

但我希望如果curosor将按钮徘徊3秒然后它应该执行点击事件。

我已经尝试了一个计时器,但我无法停止计时器,并且它连续随机地调用timer_tick ......







编辑代码..



i have a C# winForms code that when mouse hovers over a button then it immediately triggers a click event of a button..
but i want that if curosor hovers a button for 3 seconds then then it should perform click event.
I have tried a timer but i am unable to stop a timer nd it calls timer_tick randomly nd continuously...



Edited code..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication23
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();

            //this.MouseHover += new System.EventHandler(this_MouseHover);
            button1.MouseHover += new EventHandler(button1_MouseHover);
            button1.MouseLeave += new EventHandler(button1_MouseLeave);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button clicked after 3 seconds",counter.ToString());
        }

        private void button1_MouseHover(object sender, EventArgs e)
        {
            label1.Text = "Mouse hover";

            counter =0;
            //timer1.Interval = 1000 - SystemInformation.MouseHoverTime;
            //Timer timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick_1); // Everytime timer ticks, timer_Tick will be called
            timer1.Interval = 1000;
            timer1.Start();
        }




        int counter;


        private void button1_MouseLeave(object sender, EventArgs e)
        {
            timer1.Stop();
            //MessageBox.Show("leave",counter.ToString());
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            counter++; button1.Text = counter.ToString();
            if (counter==3)
            {
                timer1.Stop();


                button1.PerformClick();

            }
        }




    }
}

推荐答案

imho,日期的答案错过了一个基本问题:用于区分鼠标在控件上的3秒间隔的关键事件是MouseEnter,而不是MouseHover。



1.在设计时:



a。在窗体上放置一个Button Control,'button1:连接其MouseEnter和MouseLeave EventHandlers,如下所示。



b。在表单上放置一个TextBox控件,'textBox1:将其MultiLine属性设置为'true。将其ScrollBar属性设置为Vertical。



c。在表单上放置一个Timer组件,'timer1:连接其Tick EventHandler,如下所示。



2.代码:
imho, answers to date have missed a fundamental issue: the critical Event to use to discriminate a 3-second interval where the Mouse is over a Control is MouseEnter, not MouseHover.

1. at design-time:

a. put a Button Control on the Form, 'button1: wire up its MouseEnter, and MouseLeave EventHandlers as shown below.

b. put a TextBox Control on the Form, 'textBox1: set its MultiLine property to 'true. Set its ScrollBar property to 'Vertical.

c. put a Timer Component on the Form, 'timer1: wire up its Tick EventHandler as shown below.

2. The code:
private bool overTheButton;

private bool hoverOverTheButtonEventTriggered = false;

private void button1_MouseEnter(object sender, EventArgs e)
{
    overTheButton = true;
    hoverOverTheButtonEventTriggered = false;
    timer1.Start();
}

private void button1_MouseLeave(object sender, EventArgs e)
{
    overTheButton = false;
    hoverOverTheButtonEventTriggered = false;
    timer1.Stop();
}

private void button1_Click(object sender, EventArgs e)
{
    if (hoverOverTheButtonEventTriggered)
    {
        textBox1.Text += "3 second hover event triggered" + Environment.NewLine;
        hoverOverTheButtonEventTriggered = false;
    }
    else
    {
        textBox1.Text += "Regular click" + Environment.NewLine;
        timer1.Stop();
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Stop();

    if (overTheButton && ! hoverOverTheButtonEventTriggered)
    {
        hoverOverTheButtonEventTriggered = true;
        button1.PerformClick();
    }
}

3。运行代码:观察如果在鼠标超过按钮3秒之前单击按钮会发生什么。观察如果在鼠标悬停在三秒钟之前单击按钮会发生什么,并且不要移动鼠标(它仍然在按钮上方)。观察如果将鼠标移动到按钮上三秒钟后鼠标停留在按钮上,并在发生前3秒钟点击后将鼠标悬停在按钮上......等等。



4.注意这里的假设:



a。程序员想要做一件事,如果Button得到一个Click因为Timer已经过去了,可能还有另一件事,如果在Timer过去之前点击了Button.



湾如果用户将鼠标悬停在按钮上,并且触发了一个Timer-elapsed Click事件:程序员不想再次启动Timer,因为鼠标仍然在Button上。



c。如果用户在鼠标悬停三秒之前点击按钮:程序员不希望触发基于计时器的事件,因为鼠标停留在按钮上。



~



如果您的用例没有做出相同的假设:请更改此代码:)

3. Run the code: observe what happens if you click the Button before the Mouse has been over the Button 3 seconds. Observe what happens if you click the Button before three-seconds of having the Mouse over it, and don't move the Mouse (it remains over the Button). Observe what happens if you move the Mouse over the Button for three seconds and leave the Mouse over the Button after the first 3-second Click occurs ... and so forth.

4. Note the assumptions made here:

a. the programmer wants to do one thing if the Button gets a Click because the Timer elapsed, and, possibly, another, if the Button is clicked before the Timer elapses.

b. in the case the user leaves the Mouse over the Button, and a Timer-elapsed Click event is triggered: the programmer does not want to start the Timer again just because the Mouse remains over the Button.

c. in the case the user clicks on the Button before three-seconds of having the Mouse over it: the programmer doesn't want a Timer based event to be triggered just because the Mouse stays over the Button.

~

If your use-case doesn't make the same assumptions: please, change this code :)


好的,我删除了我发布的最后一个解决方案,因为它不太正确,我发现了解决方案的另一个问题。以下代码对我有用。我所做的更改是添加_isTimerRunning变量以防止重复启动计时器。第二件事发生了变化,我将你将事件附加到计时器的行移动到构造函数中。每次鼠标悬停按钮时,您都附加了一个新的事件处理程序。将它放在表单的构造函数中只会附加一次。

OK, I deleted my last solution posted as it was not quite right and I identified another issue with your solution. The below code worked for me. The changes I made are, added the _isTimerRunning variable to prevent the timer from being repeatedly started. The second thing changed, I moved the line where you attach the event to the timer into the constructor. You were attaching a new event handler every time the mouse would hover the button. Putting it in the form's constructor will only attach the even once.
timer1.Tick += new EventHandler(timer1_Tick_1);



这是代码对我有用...


Here is the code that worked for me...

public partial class Form1 : Form
    {
        bool _isTimerRunning = false;
        int counter = 0;

        public Form1()
        {
            InitializeComponent();

            button1.MouseHover += new EventHandler(button1_MouseHover);
            button1.MouseLeave += new EventHandler(button1_MouseLeave);

            timer1.Tick += new EventHandler(timer1_Tick_1);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button clicked after 3 seconds",counter.ToString());
        }
 
        private void button1_MouseHover(object sender, EventArgs e)
        {
            label1.Text = "Mouse hover";
            if (!_isTimerRunning)
            {
                _isTimerRunning = true;                
                counter = 0;                
                timer1.Interval = 1000;
                timer1.Start();
            }
        }        
 
        private void button1_MouseLeave(object sender, EventArgs e)
        {
            timer1.Stop();
            counter = 0;
            _isTimerRunning = false;
            label1.Text = "Mouse leave";
            button1.Text = counter.ToString();
        }
 
        private void timer1_Tick_1(object sender, EventArgs e)
        {
             counter++; 
            button1.Text = counter.ToString();
            if (counter==3)
            {
                timer1.Stop();
                _isTimerRunning = false;
                counter = 0;
                button1.PerformClick(); 
            }
        }
    }





此外,您的计数器从0开始并转到3实际上是4秒而不是3。



Also, your counter starts at 0 and goes to 3 which is going to actually be 4 seconds not 3.


参考这些链接。它可能会有所帮助



定时器C#。启动,停止并获取通话之间的时间量 [ ^ ]



C# - 你如何停止计时器? [ ^ ]



问候.. :)
Refer these links. It might be helpful

Timer C#. Start, stop, and get the amount of time between the calls[^]

C# - how do you stop a timer?[^]

Regards..:)


这篇关于如果鼠标悬停3秒钟,请单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆