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:
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...