7

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!

TheScholar
  • 2,527
  • 5
  • 23
  • 25
  • http://msdn.microsoft.com/en-US/library/system.windows.forms.button.doubleclick(v=vs.80).aspx – JWiley Nov 21 '12 at 04:23

4 Answers4

22

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)  {
    }
}
shf301
  • 31,086
  • 2
  • 52
  • 86
  • 2
    can you tell me how to link these two classes DoubleClickButton and Form1, i mean how we set the ControlStyles of a button on Form1 from a different class ? – Mogli Nov 11 '13 at 12:45
  • i'm no expert but setstyle can also be used without inheritance see answer here http://stackoverflow.com/questions/36771882/how-can-i-make-a-button-that-can-sendkeys-into-a-datagridview-so-i-need-to-some – barlop Sep 01 '16 at 14:43
  • Assuming I have a group of buttons that will require doubleClick event. Do I have to add them one by one? Or there's a way to get all the group buttons and assign them into that method? I was trying to to this but I'm unable to set the event to the group buttons. –  Apr 05 '18 at 11:05
0

Use this. Code works.

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

 DoubleClickButton button = new DoubleClickButton();
 button.DoubleClick += delegate (object sender, EventArgs e)
        {
           //codes
        };
Yağmur Kopar
  • 101
  • 1
  • 4
-1

i used to add MouseDoubleClick event on my object like:

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

elle0087
  • 840
  • 9
  • 23
  • Please edit your answer to include some explanation. Code-only answers do very little to educate future SO readers. Your answer is in the moderation queue for being low-quality. – mickmackusa Apr 18 '17 at 10:23
-2

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
})
Steven
  • 29
  • 1
  • 3
    Do not do this! You'll be ignoring the user's double-click speed setting. Windows will tell you if it was a double-click if you follow the other answer to derive from Button and create a double-clickable button. – Eric Falsken Mar 15 '13 at 02:42
  • It has nothing to do with doublé click – Diego Dec 17 '17 at 00:35