Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a custom button class in order to determine whether click or double click event takes place. But I don't know how to fire the appropriate events. Maybe I am wrong. I would like to explain you the reason I need that kind of functionallity. Well I have 3 buttons on a form, the btnSearch executes a SQL query and brings data to a datagridview, different data are displayed every time I press the button, because I select some days between two DatePickers, the btnGraph displays the appropriate graph according the data of the datagridview and the btnPrint displays appropriate the crystal report with the data of DataGridView and the graph of course. So everything works fine but if the user double clicks all the time to btnSearch or btnGraph, I am getting an error message "Error in file temp, Invalid Argument for database" I click ok and then the report is being load properly. So I need to avoid user from anauthorized double clicking multiple times on the buttons.
So how can I fire DoubleClickButton and SimpleClickButton of the customButton1 from the form? Any explanation will be much appreciated.
Thank you so much in advanced.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AngelTechShop
{
    public class CustomButton : Button
    {
        private bool mClicked, mDoubleClicked;
        public event EventHandler DoubleClickedBtn;
        public bool Clicked
        {
            get { return mClicked; }
            set { mClicked = value; }
        }

        public bool DoubleClicked
        {
            get { return mDoubleClicked; }
            set { mDoubleClicked = value; }
        }

        public CustomButton()
        {
            this.Font = new Font("Tahoma", 9F, 
System.Drawing.FontStyle.Bold);
            this.ForeColor = Color.Blue;
            this.BackColor = Color.FromArgb(153, 180, 209);
            this.DoubleClick += new EventHandler(DoubleClickButton);          
        }

        public virtual void OnDoubleClickedBtn(object sender,EventArgs e)
        {
            DoubleClickedBtn?.Invoke(this, e);
        }
        public delegate void DoubleClickButtonEventHandler(object sender, EventArgs e);
        
        public  void DoubleClickButton(object sender, EventArgs e)
       {
            SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
            DoubleClicked = true;
            Clicked = false;
            MessageBox.Show("Double Clicked");
       }

        public void SimpleClickButton(object sender, EventArgs e)
        {
            SetStyle(ControlStyles.StandardClick | ControlStyles.StandardClick, true);
            Clicked = true;
        }
    }
}


I tried on the form load event
customButton1.OnDoubleClickedBtn(sender,e);
with no effect (the event doesn't fire) in order to check if the button is double clicked. Where am I wrong?
Thank you so much in advanced.


I had a notification from an answer but it doesn't displayed at all. However, I would like to explain the reason that both solution don't work. Let's be more clear!
If I change the class to the following
public class DoubleClickButton : Button
    {
        private bool mDoubleClicked, mSimpleClicked;
        public bool DoubleClicked
        {
            get { return mDoubleClicked; }
            set { mDoubleClicked = value; }
        }
        public bool SimpleClicked
        {
            get { return mSimpleClicked; }
            set { mSimpleClicked = value; }
        }

        public DoubleClickButton()
        {
            this.Font= new System.Drawing.Font("Tahoma",9F,System.Drawing.FontStyle.Bold);
            this.SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
        }
    }

And in the form load event I set the following
doubleClickButton1.DoubleClick += DoubleClickButton1_DoubleClick;
doubleClickButton1.Click += doubleClickButton1_Click;

Double click is alright being raised, but when double click is being raised, click event is being raised by default too. So, I don't want the click event to be raised at all when I double click to the doubleClickButton1, but I want the click event to be raised only on the click event.
private void doubleClickButton1_Click(object sender, EventArgs e)
{
    if (!doubleClickButton1.DoubleClicked)
        Search();
}

public void DoubleClickButton1_DoubleClick(object sender, EventArgs e)
{
    doubleClickButton1.DoubleClicked = true;
    MessageBox.Show("Double Clicked");
    doubleClickButton1.DoubleClicked = false;
}

When I double click on the DoubleClickButton1 the messageBox is displayed, but the Search method is called too, because both events are raised simultaneously. If I remove
doubleClickButton1.DoubleClicked = false;
of the DoubleClickButton1_DoubleClick, then if I simple click on the doubleClickButton1 the Search method is never executed. However the first time the form is loaded, Click is being raised when I click on the doubleClickButton1.
I need to disable successively double clicks even after the first click and after the first execution, even any other successively double clicks, but I can't do it, because Click Event is always being fired when I double click on the button. I don't need to be fired when Double Click event is fired, but I need to be fired when I simple click on the button. It looks complicated...
Posted
Updated 6hrs 10mins ago
v6
Comments
0x01AA yesterday    
If I understand your problem correctly, there is a much more easy way to solve it (at least I think). Simply disable the respective button at the start of the button event handler and enable it at the end (or maybe in OnIdle)
Aggeliki Asimakopoulou yesterday    
Excuse me, I don't understand it. May you explain me better please?
I don't need to disable searchBtn, the user chooses between two dates and should be always enabled in order to display the appropriate data and graph on the form and then on the crystal report. But I want to prevent anauthorized double clicking multiple times continuously.
0x01AA yesterday    
To avoid doubleClick you can do sometging like (pseudo code)
buttonXYZ.OnClickedBtn(sender,e)
{
buttonXYZ.Enabled= false;
try
{
// Do the work here...
}
finally
{
buttonXYZ.Enabled= true;
}
}
Aggeliki Asimakopoulou yesterday    
What does try and finally do?
0x01AA yesterday    
In case something went wrong in the 'try block' ('// Do the work here...'), e.g. throws an exception, the __finally block will executed anyway. Means, you can be sure Button buttonXYZ will be enabled, no matter what happend.

Btw. please use 'Reply' button on a comment. This ensures, the sender of the comment will be notified, that you answered ;)

1 solution

To address the issue you're facing with double clicks on buttons causing errors, you can implement functionality to differentiate between a single click and a double-click in your custom button class, or you can simply disable the button during processing to prevent repeated clicks.

Here’s a step-by-step guide to handle this problem:

Solution 1: Using a Custom Button Class to Handle Click and Double-Click
You can create a custom button that distinguishes between single and double clicks using timers. The idea is to wait briefly after a click to see if the user double-clicks before firing the single-click event.

Here’s an example implementation:

csharp
Copy code
C#
public class CustomButton : Button
{
    private System.Timers.Timer clickTimer;
    private bool isDoubleClick = false;
    private const int DoubleClickThreshold = 300; // Time in ms to distinguish between clicks

    public event EventHandler SingleClickButton;
    public event EventHandler DoubleClickButton;

    public CustomBut
 
Share this answer
 
v3
Comments
Aggeliki Asimakopoulou 6hrs 10mins ago    
Thank you so much.
I would like to explain the reason that both solution don't work. Let's be more clear!
If I change the class to the following
public class DoubleClickButton : Button
{
  private bool mDoubleClicked, mSimpleClicked;
  public bool DoubleClicked
  {
      get { return mDoubleClicked; }
      set { mDoubleClicked = value; }
  }
  public bool SimpleClicked
  {
     get { return mSimpleClicked; }
     set { mSimpleClicked = value; }
  }

  public DoubleClickButton()
  {
     this.Font= new System.Drawing.Font("Tahoma",9F,System.Drawing.FontStyle.Bold);
     this.SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
  }
}

And in the form load event I set the following
<pre>
doubleClickButton1.DoubleClick += DoubleClickButton1_DoubleClick;
doubleClickButton1.Click += doubleClickButton1_Click;


Double click is alright being raised, but when double click is being raised, click event is being raised by default too. So, I don't want the click event to be raised at all when I double click to the doubleClickButton1, but I want the click event to be raised only on the click event.
private void doubleClickButton1_Click(object sender, EventArgs e)
{
    if (!doubleClickButton1.DoubleClicked)
        Search();
}

public void DoubleClickButton1_DoubleClick(object sender, EventArgs e)
{
    doubleClickButton1.DoubleClicked = true;
    MessageBox.Show("Double Clicked");
    doubleClickButton1.DoubleClicked = false;
}


When I double click on the DoubleClickButton1 the messageBox is displayed, but the Search method is called too, because both events are raised simultaneously. If I remove
doubleClickButton1.DoubleClicked = false;
of the DoubleClickButton1_DoubleClick, then if I simple click on the doubleClickButton1 the Search method is never executed. However the first time the form is loaded, Click is being raised when I click on the doubleClickButton1.
I need to disable successively double clicks even after the first click and after the first execution, even any other successively double clicks, but I can't do it, because Click Event is always being fired when I double click on the button. I don't need to be fired when Double Click event is fired, but I need to be fired when I simple click on the button. It looks complicated...
Richard Deeming 3hrs 20mins ago    
Don't hold your breath - you're replying to a spammer.

The first version of this "solution" had a payload link at the bottom, which was removed by an editor.

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