SIGN UP MEMBER LOGIN:    
ARTICLE

How to Handle Double-Click in Silverlight 5

Posted by Jaganathan Bantheswaran Articles | Silverlight with C# May 25, 2011
In this article we will discuss how to handle a double-click in Silverlight 5. Handling double-click is a new feature in Silverlight 5.
Reader Level:
Download Files:
 


Prologue:

In this article we will discuss how to handle a double-click in Silverlight 5. Handling double-click is a new feature in Silverlight 5.

Silverlight 5 has introduced the concept of a click count. Rather than create dedicated double, triple, or multiple click events, you can simply count the number of clicks using the ClickCount property of the MouseButtonEventArgs class. This works for both the left and right mouse buttons.

Setup the Solution:

Create a Silverlight Application project with the name "HandlingDoubleClickInSL" as shown below.

DCSil1.gif

As we are going to demonstrate in Silverlight 5 just ensure that we have selected Silverlight 5 in the project's options when creating the project.

DCSil2.gif

Setting up the XAML:

Open the MainPage.xaml to design our screen. Here are the steps to create our UI.

  1. Divide the LayoutRoot grid into two columns as in the following figure.

    DCSil3.gif
     
  2. Add two StackPanels to the Grid; one for Grid column 0 and another one for Grid column 1.
  3. In the first StackPanel Add 5 Borders with Height as 100.
  4. In the second StackPanel add a TextBlock with the following properties.

    DCSil4.gif

Adding Mouse Click Event to Border:

Now just add the "MouseLeftButtonDown" in the Border's property with Event name like as shown in the figure.

DCSil5.gif

Complete Xaml:

The given howfollowing figure shows the complete code for our UI.

DCSil6.gif

ClickCount Event:

We have defined the Event as in the figure shown.

DCSil7.gif

Here the bdr.Tag.ToString () is used to get the border that we clicked. The e.ClickCount is the Property of MouseButtonEventArgs class which is of type integer.

The e.ClickCount == 2 handles the double click condition.

The ClickCount Property maintains the Clicks count for us.

How ClickCount Calculated:

The count is calculated based on the time between first click and the second click.

Consider if you are trying to click 5 times. After 3rd click, if you give 200 milliseconds gab between the 3rd and 4th click then the 4th click will be treated as the 1st click. It will reset the ClickCount property if the time between the first click and second click is greater the 200 milliseconds.

Application in Action:

The logic in this demo is when you are clicking on various colour boxes with various clicks it will display the colour box name with the clicks you have clicked.

Let see the Demo.

DCSil8.gif

In this figure above the single click is captured.

DCSil9.gif

The above figure shows the double-click on the Red color box.

You can see the Multiple clicks displayed in the following figure.

DCSil10.gif

Issues faced with Click Event:

As Kunal has said we have 2 issues in MouseLeftButtonDown Event.
  1. Debugging the Event.
  2. Handling more clicks in an event.

As I have faced the issues mentioned here, I am sharing here. For more details on this issue, visit Kunal's article.

Summary:

In this article, we have seen how to handle single, double and multiple clicks in Silverlight 5.

We can handle multiple clicks but not multiple times.

Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which enables me to give a better article the next time.

Please rate this Article.

Thanks.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks dinesh.

Posted by Jaganathan Bantheswaran May 25, 2011

Good article Jaganathan, thanks for sharing

Posted by Dinesh Beniwal May 25, 2011

Screenpresso. its free. download from www.screenpresso.com/download.html.

Posted by Jaganathan Bantheswaran May 25, 2011

me too started with SL5 :). This a good article, good sample and good screen shots (which tool used for editing images).

Posted by Suthish Nair May 25, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor