Click here to Skip to main content
15,304,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to use actual double click event for buttons
i cant use mouse down event and e.clicks =2 for double click because i have some code on mouse up event which needed to be executed i have found one link for button double click

Button.DoubleClick Event (System.Windows.Forms)[^]

here it states that ControlStyles.StandardDoubleClick are set to false for button so is there any way to set it true and use doubleclick event or any other way for button double click

What I have tried:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As MouseEventArgs)

If e.Clicks = 2 Then
MsgBox(e.Clicks.ToString)
end if
end sub
Posted
Updated 18-Apr-16 22:48pm
v2

1 solution

This is not a good UI practice - hence why it's hidden and not easy to change.
Derive a new control from Button, and use SetStyle to allow them both:
C#
public class MyButton : Button
    {
    public MyButton()
        {
        SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
        }
    }
You must enable both styles for double click to work.
Then you can add the button, and manually add the event handler:
C#
MyButton mb = new MyButton();
mb.Text = "My Button";
mb.Size = new Size(100, 30);
mb.Location = new Point(100, 100);
mb.Visible = true;
Controls.Add(mb);
mb.DoubleClick += MyButton_DoubleClick;
You can't add the handler via the Properties pane in the designer, as it's a hidden event.
   
Comments
Omkaara 19-Apr-16 4:54am
   
@OrignalGriff will you please explain why its hidden event or not a good UI practice what sort of prbs can occur
OriginalGriff 19-Apr-16 5:52am
   
I didn't write the .NET framework - you'd have to talk to MS! :laugh:
But a button isn't meant to support double click - because it's "normal" click action is often to close the form and either OK or Cancel it, I suspect - so a double click on a button is not something a user is expecting to do. Think about it: when was the last time you double clicked a Windows or website button?
If it's unusual, then it's probably not a good idea - because it tends to confuse users, and that means they either can't use your app or dislike it.
In order for double click to do something, click probably shouldn't - and a button that doesn't do anything when you click it is pretty annoying! :laugh:
Now, I have no idea what you think you need this - I don't know what the heck you are using it for - but generally if you are going outside "normal" UI rules, then you've made a mistake somewhere!
Omkaara 19-Apr-16 6:06am
   
ahh thanx a lot!!
i am trying to create UI designer i need it for dynamic control handling
OriginalGriff 19-Apr-16 6:48am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900