Friday, April 22, 2011

Silverlight 5 : Multiple Click Support aka ClickCount

In the last article I spend some time with you in discussing some Text related enhancements for Developers in Silverlight 5. Today I am going to discuss one more feature of Silverlight 5 in this short post.

Requirement : We need a mechanism to identify count of multiple mouse clicks made by user.

Scenario : Some over enthusiast end users,if the transaction/process is not happening then they always hit button/any other UI control with high enthusiasm (or sometimes anger) several amount of time, Some time these unnecessary clicks becomes headache and sometimes it is useful to trigger some business function as well. So there is good side and bad side of this.

What Silverlight 5 is offering to us? : Silverlight 5 bits are now giving support/mechanism to identify Multiple Clicks made by user.

How it does that? : Inside your class MouseButtonEventArgs which is sealed,exposes one public property of type Integer like this :


This ClickCount property gives us number of hits made by user via Mouse.

Cool..any sample ? : Here you go !


<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="MouseCount" FontSize="20" Margin="5,5,5,5" MouseLeftButtonDown="MouseCount_MouseLeftButtonDown" />   </Grid>

C# :

void MainPage_Loaded(object sender, RoutedEventArgs e)
     MouseCount.Text = "This Place will show how many times you play with Mouse !";

private void MouseCount_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
           // Checks the number of clicks.
           if (e.ClickCount == 1)
               // Single Click occurred.
               MouseCount.Text = String.Empty;
               MouseCount.Text = "You clicked Mouse Once !";

           if (e.ClickCount == 2)
               // Double Click occurred.
               MouseCount.Text = String.Empty;
               MouseCount.Text = "You clicked Mouse Twice !";

           if (e.ClickCount >= 3)
               // Triple Click occurred.
               MouseCount.Text = String.Empty;
               MouseCount.Text = "You clicked Mouse more than 2 times !";
Remember e.ClickCount is a key ! it will give you number of clicks.

Will it work? : Yes ! Here is the output starting from no click to more than 2 clicks …





Wait ! All that glitters is not Gold !! :

My dear friend and fellow Silverlight MVP Kunal Chowdhury written a quick article on “Issues with Multiple Clicks in Silverlight 5”, so read that here and get to know how this e.ClickCounts make life bit difficult for Developers like you and me.

What’s the Problem ? – Debugging ! :

If you observe the code snippet above and article by Kunal which I shared above,you will find that there is a great trouble to your brain while debugging since despite of e.ClickCount > 1 still it always hits e.ClickCount = 1 condition and comes out of the game and Game is over for us since we really can’t debug and see the stuff for e.ClickCount > 1 and here we see the sad story !

Is it a Bug? what can be done to avoid this ? :

Well, As a MVP I see it as functional issue rather than calling it as Bug since functionality is working but issue is coming only while debugging so I feel little awkward to call it as Bug !

Condition to your Breakpoint is the only option I find it suitable at this particular moment unless someone from Microsoft don’t come up with something better to avoid this debug issue. So if I want to check / debug and see some stuff for e.ClickCount then how will I achieve this? .. well all you need to do is just right click and click on Condition like this :




Above screen shows how easy is our life ! So now be ready to debug all that e.ClickCount > 1 stuff which was giving you trouble earlier. Remember that even for Switch..Case it’s the same issue so don’t be under impression that problem will not occur with Switch..Case.

One you done with adding condition, it will be hitting your breakpoint and executing appropriate condition on basis of e.ClickCount value and you can see that output like this :


So this is bit schoolish solution but it works in such tricky conditions and we can survive for the time being.

Well,Take the positive part of this wonderful feature and how it is helpful in real business scenario, I personally still have no clue if this is officially marked as “Bug” or not,but this is what came to my observation when I was playing with the bits, I hope in coming RTW bits something will come up for this, Well enough for today and some more such nice features and funny tricky solutions and scenarios are coming up in my upcoming posts,So stay tuned !



Unknown said...

For Silverlight 5, one of the most interesting tooling enhancements is the ability to set breakpoints and debug bindings in XAML. Nice Post

Silverlight Consulting

Unknown said...

informative post, it would help me to understand multiple click support.

IT Outsourcing Company India

Anonymous said...

Hello Vikram,

I read your article about Silverlight 5 Multiple Click etc. such a top class written with showing there tool improvements. Very useful sharing for software developers.

PHP Developer at
Calipus Software pvt. Ltd.

Andrew said...

Silverlight 5 is still in very good shape and many companies are using it.

Thanks for detailing out the features of Silverlight 5.

Software Development Company - Calipus