Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #82 – How to Implement Double Click

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 thing to do is to start a DispatcherTimer timer once a left mouse click event has been received. If another mouse click is intercepted before the double click time interval has passed then a double click has occurred. This interval is typically set to be around 200 milliseconds. Once 200 milliseconds has passed the timer is stopped and disabled until another mouse click is received.

To start, let’s create our timer and add a listener for the MouseLeftButtonDown event.

DispatcherTimer _doubleClickTimer;
Image _lastImage = null;
 
public Page()
{
    InitializeComponent();
   
    _doubleClickTimer = new DispatcherTimer();
    _doubleClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 200);
    _doubleClickTimer.Tick += new EventHandler(DoubleClick_Timer);
 
    this.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
}
 
// too much time has passed for it to be a double click.            
void DoubleClick_Timer(object sender, EventArgs e)
{
    _doubleClickTimer.Stop();
}

Now, in the Page_MouseLeftButtonDown() method we:

  1. Check if the timer is enabled.
  2. If it is enabled already, then we have already clicked once and have made a double click.
  3. If is it not enabled start the timer.
void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (_doubleClickTimer.IsEnabled)
    {
        // a double click has occured
        _doubleClickTimer.Stop();
 
        Image catImg = new Image();
        Uri uri = new Uri("cat.png", UriKind.Relative);
        ImageSource imgSource = new
            System.Windows.Media.Imaging.BitmapImage(uri);
        catImg.Source = imgSource; 
 
        catImg.SetValue(Canvas.LeftProperty,(double) e.GetPosition(LayoutRoot).X-90);
        catImg.SetValue(Canvas.TopProperty, (double)e.GetPosition(LayoutRoot).Y-113);
 
        if(null != _lastImage)
            MainCanvas.Children.Remove(_lastImage);
        
        MainCanvas.Children.Add(catImg);
        _lastImage = catImg;
 
    }
    else
    {
        _doubleClickTimer.Start();
    }
}

Here is a demo of this code:

 

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Silverlight currently has full mouse support for single click. However, double click is a another story

# January 15, 2009 5:57 PM

miguelito928 said:

Your demo is popping up a dialog that says I don't have the latest Silverlight install.  When I follow the redirection to download the latest, it tells me I've already got the latest.

Any ideas?

# January 15, 2009 6:10 PM

TedHoward said:

Version issues on this page? When it loads for me, a dialog box appears telling me that my Silverlight install has expired. Clicking "Yes" in that dialog, which is not asking a question btw, takes me to a Microsoft hosted page (www.microsoft.com/.../install.aspx) which tells me that my Silverlight install is up to date (v2.0.31005.0).

Mac OS X 10.5.6

Firefox 3.3.5

# January 15, 2009 6:13 PM

mike.snow said:

Fixed. Let me know if you see any further issues with it.

Thanks.

# January 15, 2009 6:42 PM

Community Blogs said:

In this issue: John Stockton, Adam Kinney, Koen Zwikstra, Matthias Shapiro, Swiss MSDN Team Blog, Jordan

# January 16, 2009 1:30 AM

Simbalight said:

I recognized a small difference to the behaviour we all have got used to. Double click should only be recognized, if the mouse cursor position didn't change within the 200 ms.

# January 16, 2009 2:41 AM

Silverlight Travel » How to Implement Double Click said:

Pingback from  Silverlight Travel » How to Implement Double Click

# January 16, 2009 3:19 AM

shinedraw said:

Hi Mike,

You may also be interested about my double post as well. Here it is

www.shinedraw.com/.../silverlight-vs-flash-double-clicking

The methods we used are very similar

# January 16, 2009 4:35 AM

mike.snow said:

ShineDraw-

Love your site! Very cool applications...

Simbalight - You are right about the mouse movement. I will add a check to verify the mouse did not move.

Thanks.

# January 16, 2009 11:19 AM

Dew Drop – January 17, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop – January 17, 2009 | Alvin Ashcraft's Morning Dew

# January 17, 2009 9:27 PM

Implementieren des Doppelklick at Programming with Silverlight, WPF & .NET said:

Pingback from  Implementieren des Doppelklick at Programming with Silverlight, WPF & .NET

# January 18, 2009 9:29 AM

Odegaard said:

It's not a dblclick if the mouse moves between the two clicks, but your code allows for that.

# January 19, 2009 3:10 PM

mike.snow said:

Yes, as stated in my comment above I will be adding a check for this soon. Thanks!

# January 19, 2009 3:23 PM

Odegaard said:

# January 19, 2009 4:23 PM

Frank La Vigne said:

# January 21, 2009 10:09 PM

Frank La Vigne said:

# January 21, 2009 10:10 PM

Frank La Vigne said:

# January 22, 2009 12:33 PM

Frank La Vigne said:

# January 23, 2009 11:27 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight Tips of the

# January 23, 2009 7:21 PM

sildever said:

I'm getting a message that states "Server is too busy"  just below the line "Here is a demo of this code:"

# January 28, 2009 10:02 AM

Visual Web Developer Team Blog said:

Most Recent Posts: Tip #93 - Reading XML with Silverlight Tip #92 - How to Load Images from a Stream

# February 10, 2009 6:25 PM

Scosby Blog said:

Mouse double click in Silverlight 2

# March 4, 2009 2:34 PM

Angelica_Miranda said:

If the control I want to DoubleClick doesn't fire MouseLeftButtonDown event what I can do? Any suggestions?

# June 4, 2009 8:50 PM

mike.snow said:

Did you check for the event Click ?

# June 5, 2009 12:40 PM

Silvelight Resources Directory Update (13 Aug) said:

Pingback from  Silvelight Resources Directory Update (13 Aug)

# August 13, 2009 1:09 AM

Twitter Trackbacks for Silverlight Tip of the Day #82 ??? How to Implement Double Click - Silverlight Tips of the Day - Blog by Mike [silverlight.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Silverlight Tip of the Day #82 ??? How to Implement Double Click - Silverlight Tips of the Day - Blog by Mike         [silverlight.net]        on Topsy.com

# August 28, 2009 10:03 AM

HeroBeast said:

MikeSnowrecentlypostedanarticleonhowtododoubleclicksinSilverlight.However,hisapproac...

# September 2, 2009 11:28 PM