WinForms how to call a Double-Click Event on a Button?

我们两清 提交于 2019-11-27 03:36:18

问题


Rather than making an event occur when a button is clicked once, I would like the event to occur only when the button is double-clicked. Sadly the double-click event doesn't appear in the list of Events in the IDE.

Anyone know a good solution to this problem? Thank you!


回答1:


No the standard button does not react to double clicks. See the documentation for the Button.DoubleClick event. It doesn't react to double clicks because in Windows buttons always react to clicks and never to double clicks.

Do you hate your users? Because you'll be creating a button that acts differently than any other button in the system and some users will never figure that out.

That said you have to create a separate control derived from Button to event to this (because SetStyle is a protected method)

public class DoubleClickButton : Button
{
    public DoubleClickButton()
    {
        SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
    }
}

Then you'll have to add the DoubleClick event hanlder manually in your code since it still won't show up in the IDE:

public partial class Form1 : Form {
    public Form1()  {
        InitializeComponent();
        doubleClickButton1.DoubleClick += new EventHandler(doubleClickButton1_DoubleClick);
    }

    void doubleClickButton1_DoubleClick(object sender, EventArgs e)  {
    }
}



回答2:


i used to add MouseDoubleClick event on my object like:

this.pictureBox.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseDoubleClick);




回答3:


A double click is simply two regular click events within a time t of each other.

Here is some pseudo code for that assuming t = 0.5 seconds

button.on('click' , function(event) {
    if (timer is off or more than 0.5 milliseconds)
        restart timer
    if (timer is between 0 and 0.5 milliseconds)
        execute double click handler
})


来源:https://stackoverflow.com/questions/13486245/winforms-how-to-call-a-double-click-event-on-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!