Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 687
TheScholar Avatar asked Nov 21 '12 04:11

TheScholar


People also ask

How do you call a button's Click event one form to another form?

Solution 1 If you need a Button to Click in Form due to something happening in Form2 then Form2 should raise an event that Form subscribes to, and in the handler for that event it should either call the handler for the Button 's Click or call the Button 's PerformClick() method.

When the double-click event occurs on the form control?

The Click event is raised every time a control is double-clicked. For example, if you have event handlers for the Click and DoubleClick events of a Form, the Click and DoubleClick events are raised when the form is double-clicked and both methods are called.

What is used to perform a Click event in Windows Forms?

Button control is used to perform a click event in Windows Forms, and it can be clicked by a mouse or by pressing Enter keys. It is used to submit all queries of the form by clicking the submit button or transfer control to the next form.


1 Answers

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)  {
    }
}
like image 152
shf301 Avatar answered Sep 20 '22 18:09

shf301