The ClickCount concept was introduced in silverlight5 . This makes support for multi-click input on left and right mouse button . This makes our life easy to implement features like double click or triple click . The ClickCount property is available on MouseButtonEventArgs class. This property is used to count number of clicks for mouse left or right button.Now SL5 does the job for us and makes easy , we do not need to capture or fire events separately for double click or triple click. So now let us see how to implement it in action
First we need to have Silverlight5 tools for VS2010 Sp1 from here .
Once you download tools and install , Create New Silverlight web project .
XAML Code
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Column="0" Margin="10" Height="100" Width="100" Fill="Red" MouseLeftButtonDown="Border_MouseLeftButtonDown"></Rectangle>
<ComboBox x:Name="cboInsert"
Grid.Column ="1" Margin="1" Height="20" Width="123">
</ComboBox>
<Button x:Name="btnReset" Margin="1" Grid.Row="2" Click="btnReset_Click" >Reset</Button>
<TextBlock x:Name="txtBlock" Grid.Column="3" Height="123" Width="100">Hima</TextBlock>
</Grid>
</UserControl>
Implementing Multiple Click
It is such simple and easy to implement, here is the code for that.
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
cboInsert.Items.Clear();
txtBlock.Text = "";
}
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cboInsert.Items.Add(e.ClickCount);
if (e.ClickCount == 0)
{
txtBlock.Text = "Nothing is clicked";
}
else if (e.ClickCount == 1)
{
txtBlock.Text = "Single Click";
}
else if (e.ClickCount == 2)
{
txtBlock.Text = "Double Click";
}
else if (e.ClickCount == 3)
{
txtBlock.Text = "Triple Click";
}
else
{
txtBlock.Text = "Clicked " + e.ClickCount + " times";
// MessageBox.Show("Clicked " + e.ClickCount + " times");
}
}
}
Output
Click on the rectangle using Mouse , the following output displays when we click on it 7 times.
I just added textbclock to show the clicked items, reset button to reset items in dropdown and text block . The code is self explanatory , e.ClickCount gets the count of mouse left button click. So now we can write what ever so the logic once the event get captured.
Note
There is no limit for the click. you can try clicking slowly and fast by playing with it.
This is mainly useful in Gaming, 3D, Music , Specialized programs .
These are the events supported MouseLeftButtonDown, MouseLeftButtonUp, MouseRightButtonDown, MouseRightButtonUp