C# .NET - Button Double-Click - Asked By Patrick Skelton on 26-Sep-08 11:41 AM

Hi,

Can anyone tell me if it is possible to get the double-click event in a Windows.Forms.Button?  I notice this event exists in the base Control class, but overrriding OnDoubleClick() doesn't catch the event.  How is this being filtered out, and is it possible to bypass the filtering?

Any help or advice would be very much appreciated.

Best wishes, Patrick


solution - Perry replied to Patrick Skelton on 26-Sep-08 11:52 AM

The following code example demonstrates the use of this member. In the example, an event handler reports on the occurrence of the DoubleClick event. This report helps you to learn when the event occurs and can assist you in debugging. To report on multiple events or on events that occur frequently, consider replacing MessageBox.Show with Console.WriteLine or appending the message to a multiline TextBox.

To run the example code, paste it into a project that contains an instance of type Button named Button1. Then ensure that the event handler is associated with the DoubleClick event. refer http://msdn.microsoft.com/en-us/library/system.windows.forms.button.doubleclick.aspx for details.

private void Button1_DoubleClick(Object sender, EventArgs e) {

   MessageBox.Show("You are in the Button.DoubleClick event.");

}

-Paresh

Button Double-Click - Sagar P replied to Patrick Skelton on 27-Sep-08 12:53 AM

VS 2003 actually has the double-click event built into the button and
treeview controls so if you upgrade, it will be there.
If that's not an option, you will probably have to write a class to inherit
from button and create the event yourself.  The code to figure out the
actual double-click event would look something like this:

    Dim LastClicked As DateTime

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Click
        Dim TimeNow As DateTime = Now
        Dim ClickDifference As Double =
TimeNow.Subtract(LastClicked).TotalMilliseconds
        If ClickDifference < 1000 And ClickDifference <> 0 Then
            RaiseEvent DoubleClick()
            MsgBox("Double-clicked")
        Else
            LastClicked = TimeNow
        End If
    End Sub

Thank you - Asked By Patrick Skelton on 29-Sep-08 06:03 AM

Thanks for all the replies.

I am using VS 2008 and still cannot see a double-click event listed in the events for a standard button.

Never mind - as it happend, I have for now found a different way round the problem.

Thanks again.

-Patrick


hi skelton - ivan perez replied to Patrick Skelton on 29-Sep-08 01:01 PM

You can do many things like you saw but i do an algorithm solution. I think if you have a access to the event onClic you can know when the user do double clic. So i do the class Is double clic? with an metod with the same name and this method return a bool if is double clic or not. I use Environment.TickCount; to know the time between clics like a chronometer and decide if is double or not.

Is_Double_Clic press = new Is_Double_Clic(500);

private void button1_Click(object sender, EventArgs e)

{

if (press.Is_this_a_double_clic()) button1.Text = "2 clics";

else button1.Text = "1 clic";

}

}

public class Is_Double_Clic

{

private int Initial_Time=0;

private int Time_to_wait;

public Is_Double_Clic(Int32 Time_Between_clics_in_milliseconds)

{

this.Time_to_wait = Time_Between_clics_in_milliseconds;

}

public bool Is_this_a_double_clic()

{

if (Initial_Time <= 0 || (Environment.TickCount - Initial_Time) > Time_to_wait)

{

Initial_Time = Environment.TickCount;

return false;

}

else

{

int auxini = Initial_Time; Initial_Time = 0;

return ((Environment.TickCount - auxini) <= Time_to_wait);

}

}

}

good appetite partners...

Thank you - Kruti Shukla replied to ivan perez on 07-Aug-09 07:29 AM
Its gr8 code.Exactly what I was looking for..It works like a charm..............

One more thing to add in this code that

Question : What if we don't want to execute the single click event when double click event is in the higher priority.

Answer: Here in the given code single click event always fires before double click is fired. For this problem I've added timer as shown below :--

private void button1_Click(object sender, EventArgs e)
        {
            if (press.Is_this_a_double_clic())
            {
                timer1.Stop();
               //do something
            }
            else
            {
                //Inside single click
                //Here I've started the timer and set the Timer interval to 500.
               //Add the code for the single click in timer1_Tick event.
               //Thus the code for single click will only fired when no other consecutive click
               //will be found.
              //This is how we can execute code for single click and double click easily
                timer1.Start();
            }
        }

 private void timer1_Tick(object sender, EventArgs e)
        {
            timer2.Stop();
          //Code for single click
         //do something

        }
jake replied to Kruti Shukla on 25-Jun-10 11:57 AM
i dont understand how to do the double click i put that code above in but i dont know here to put the link to a program on please can you help me