Click vs DoubleClick

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a control that need to perform one behavior when it's Clicked and a different behavior when it's DoubleClicked.

It seems that you can't get a DoubleClick without first getting a Click. So what's the best way to keep from executing the click functionality before it's determined if it's a double click or not?

Or do I just have to "undo" the Click stuff everytime a DoubleClick is received?

Thanks.

J
 
I have a control that need to perform one behavior when it's Clicked and
a different behavior when it's DoubleClicked.

It seems that you can't get a DoubleClick without first getting a
Click. So what's the best way to keep from executing the click
functionality before it's determined if it's a double click or not?

Or do I just have to "undo" the Click stuff everytime a DoubleClick is
received?

Yup, that's a tricky one. :) It does come up now and then, and generally
speaking the correct UI behavior is to always do the click action, and
then the double-click action if the double-click also happens.

This, of course, means that you need to define your UI so that whenever
the user is double-clicking, a single-click always makes sense. This is
*almost always* the right thing to do, but exceptions do occur. When they
do, you can either do what you propose (undo the single-click action if
you get the double-click) or you can wait out the double-click time to see
if a second click comes along, and if it doesn't then you do the
single-click action instead. The latter is what other applications
generally do.

As you can see, it's a lot easier if you can design your user-interface so
that always doing the single-click action makes sense. :)

Pete
 
Hi J,

The reason why the Click event is always raised before the DoubleClick
event of a control when the user double clicks on the control is that the
system always sends a WM_LBUTTONDOWN message to the control before sends a
WM_LBUTTONDBLCLK message.

Once the control receives the WM_LBUTTONDOWN message, the Click event of
the control is raised.

If you don't like to get the Click event fired before the DoubleClick event
is raised, I think a possible way is to hold the WM_LBUTTONDOWN message for
a short time when the control receives the this message. If the control
doesn't receive the WM_LBUTTONDBLCLK message during this short time, then
send this message out; otherwise, don't send.

The following is a sample.

using System.Runtime.InteropServices;
class MyControl : Control
{
int WM_LBUTTONDOWN = 0x0201;
int WM_LBUTTONDBLCLK = 0x0203;

Message clickMessage;
System.Windows.Forms.Timer clickTimer = new
System.Windows.Forms.Timer();
bool clickflag = false;

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,int msg, IntPtr
wParam, IntPtr lParam);

public MyControl()
{
clickTimer.Interval = 100;
clickTimer.Tick += new EventHandler(clickTimer_Tick);
}

void clickTimer_Tick(object sender, EventArgs e)
{
this.clickTimer.Stop();
clickflag = true;
SendMessage(this.Handle, clickMessage.Msg, clickMessage.WParam,
clickMessage.LParam);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN)
{
if (clickflag == false)
{
clickMessage = m;
clickTimer.Start();
}
else
{
clickflag = false;
base.WndProc(ref m);
}
}
else if (m.Msg == WM_LBUTTONDBLCLK)
{
clickTimer.Stop();
base.WndProc(ref m);
}
else
{
base.WndProc(ref m);
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks for the responses... In reviewing the interface, I considered both methods and chose option 3...

I changed the code so that one action is tied to aleft click and the other to a right click. Kinda like the old windows MineSweper game. That suits this app pretty well, anyway.

Thanks.

Jerry
I have a control that need to perform one behavior when it's Clicked and a different behavior when it's DoubleClicked.

It seems that you can't get a DoubleClick without first getting a Click. So what's the best way to keep from executing the click functionality before it's determined if it's a double click or not?

Or do I just have to "undo" the Click stuff everytime a DoubleClick is received?

Thanks.

J
 
Back
Top