One way to do this is by overriding WndProc
in a ButtonWithDoubleClick : Button
class:
class ButtonWithDoubleClick : Button
{
const int WM_LBUTTONDBLCLK = 0x203;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_LBUTTONDBLCLK:
OnDoubleClick(EventArgs.Empty);
break;
}
}
}
Replace the Button
instances in MainForm.Designer.cs
// private System.Windows.Forms.Button buttonTryDoubleClick;
private double_click.ButtonWithDoubleClick buttonTryDoubleClick;
private void InitializeComponent()
{
// this.buttonTryDoubleClick = new System.Windows.Forms.Button();
this.buttonTryDoubleClick = new double_click.ButtonWithDoubleClick();
...
}
Responding to one or two clicks
public MainForm()
{
InitializeComponent();
buttonTryDoubleClick.Click += ButtonTryDoubleClick_Click;
buttonTryDoubleClick.DoubleClick += ButtonTryDoubleClick_DoubleClick;
}
private async void ButtonTryDoubleClick_Click(object sender, EventArgs e)
{
_isDoubleClick = false;
// Allow some time to get the second click if it's coming.
await Task.Delay(100);
if(!_isDoubleClick)
{
MessageBox.Show("Clicked");
}
}
bool _isDoubleClick = false;
private void ButtonTryDoubleClick_DoubleClick(object sender, EventArgs e)
{
_isDoubleClick = true;
BeginInvoke((MethodInvoker)delegate
{
MessageBox.Show("Double Clicked");
});
}
