Silverlight currently has full mouse support for single click. However, double click is a another story. In this tip I will show you how to implement double click. You can apply this technique for an individual control or even your entire page.
The key to accomplishing this is to check for two things:
- Measure the TimeSpan between two mouse clicks. Verify it is less than around 300 milliseconds.
- Make certain the mouse has not moved more than a few pixels.
To demonstrate this I have a DateTime member that tracks the time the first mouse left click occurred. I also keep track of whether this is the first click or not.
public DateTime _lastClick = DateTime.Now;
private bool _firstClickDone = false;
Next, I add an event to monitor for left mouse clicks:
this.MouseLeftButtonDown += new
MouseButtonEventHandler(MainPage_MouseLeftButtonDown);
Finally, in the event to monitor for mouse left clicks I do the following:
- If more than 300 milliseconds has passed or a first click has not occurred yet get a snapshot of the time for the first click.
- If we have clicked already and less than 300 milliseconds has passed verify the mouse has not moved more than a few pixels. If not, we have a double click.
The following code demonstrates this technique:
private void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
UIElement element = sender as UIElement;
DateTime clickTime = DateTime.Now;
TimeSpan span = clickTime - _lastClick;
FirstClickTB.Text = "MS between clicks = " + span.TotalMilliseconds;
if (span.TotalMilliseconds > 300 || _firstClickDone == false)
{
DoubleClickTB.Text = "First click...";
_clickPosition = e.GetPosition(element);
_firstClickDone = true;
_lastClick = DateTime.Now;
}
else
{
Point position = e.GetPosition(element);
if (Math.Abs(_clickPosition.X - position.X) < 4 &&
Math.Abs(_clickPosition.Y - position.Y) < 4)
DoubleClickTB.Text = "Double Click!";
else
DoubleClickTB.Text = "Double Click failed due to mouse move!";
_firstClickDone = false;
}
}
Source: Tip16_DoubleClick.zip
[silverlight: Tip16_DoubleClick.xap]
Thank you, --Mike







Hmmmm.. in Windows at least, the default double-click time is 500ms and mouse movement is tolerated within a 4×4 rectangle around the first click. You’re quite the harsh double-clicking taskmaster, Mike!
http://blogs.msdn.com/oldnewthing/archive/2008/04/23/8417521.aspx
http://msdn.microsoft.com/en-us/library/ms724385%28VS.85%29.aspx
Personally, I’m on a mission to abolish double-clicking altogether. I’ve seen a lot of people have trouble with it, but one recent instance really takes the cake. I helped a customer whose laptop touchpad was so screwed up that he was completely unable to double-click, even when plugging an external mouse into the system. He ended up having to go into Control Panel, Mouse properties (which was overridden by the touchpad’s proprietary UI!), and change the action for the left mouse button from “click” to “double-click”. Then he was able to click and double-click normally. Yes, that’s right – from click to double-click. What sense does that make?! Ah computers… Of course, he swore he had never gone into that UI before.
Hello Mike,
there is another solution to implement the double-click: save the date/time of the previous click and calculate the time difference to detect a double-click.
This solution has the advantage to not use a timer (less resource used) and you can leave the MouseLeftButtonDown event handler.
Patrick
Its simple, but what maybe It would be much reasonable to check wheter the second click is on the same element instead of checking, if the mouse moved more than 1 pixel. Some people do have high mousesensitivity
I was JUST discussing this with a coworker (seriously, we had the conversation and I sat down to this post staring directly at me in Google Reader). It seems pretty incredible to me that there’s no built-in support for double-clicking in Silverlight 4. No support for right-clicking was understandable but even that is supported now.
It hurts my soul a little bit every time I have to do the double-click detection manually.
MojoFilter- Thanks for your feedback, I will pass it along to the Sl time! I am in full agreement
Kevkong – An element can be pretty big, the full page in fact. I would prefer that the double click is checked within a region.
Patrick – DateTime, hmm, yea maybe that’s better
I’ll try it out.
Tom- Perhaps you are right
I can update it for more tollerence.
Is there anyway to pull the mouse settings from the OS?
thanks for tip. why are you using flash on your site and not silverlight?
Chad – Not that I know of
ozz- What do you mean? All my demos are in Silverlight.
The Google ads are in Flash, but I cannot control that. Also, the WordPress plugins are flash based because they haven’t been made in SL yet.
I use DateTime approach instead. As Patric mentioned, it’s less resource intensive and is easier to implement… Timer approach sounds like using a steam-hammer to crack nuts.
PS: Sorry for criticism. I really like your tips. Keep it up!
Thanks everyone for your feedback. You are right about DateTime being better than a timer.
I have updated this tip to reflect that technique.
Let me know if you can think of even better ways to optimize it.
–Mike
I’ve written a class that could help with this in a more easier way.
http://kevindark.co.uk/SilverlightDoubleClick.aspx
Any feedback is apreciated.
Kevin.
2Tom
>in Windows at least, the default double-click time is
user changable from the config panel.
This is incradible microsoft innovation in silverlight.
the parameter see in HKEY_CURRENT_USER/Control Panel/Mouse
DoubleClickSpeed (500 is default, but user configurable)
Is it just me, or did you forget to declare _clickPosition?