Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
Controls
 How to: Handle the Checked Event fo...
Collapse All/Expand All Collapse All
Silverlight
How to: Handle the Checked Event for the CheckBox Control

When a user clicks a CheckBox control, one of three events can occur, depending on the current state of the CheckBox. Either the Checked, Unchecked or Indeterminate event occurs. You can handle these events and perform some action depending on the state of the check box.

The following code demonstrates how to handle the Checked and Indeterminate events.

Visual Basic
Private Sub HandleCheck(ByVal sender As Object, ByVal e As RoutedEventArgs)
     Dim cb As CheckBox = CType(sender, CheckBox)
     If (cb.Name = "cb1") Then
         text1.Text = "Two state CheckBox checked."
     Else
         text2.Text = "Three state CheckBox checked."
     End If
 End Sub

 Private Sub HandleUnchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
     Dim cb As CheckBox = CType(sender, CheckBox)
     If (cb.Name = "cb1") Then
         text1.Text = "Two state CheckBox unchecked."
     Else
         text2.Text = "Three state CheckBox unchecked."
     End If
 End Sub

 Private Sub HandleThirdState(ByVal sender As Object, ByVal e As RoutedEventArgs)
     Dim cb As CheckBox = CType(sender, CheckBox)
     text2.Text = "Three state CheckBox indeterminate."
 End Sub
C#
private void HandleCheck(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    if (cb.Name == "cb1")
        text1.Text = "Two state CheckBox checked.";
    else
        text2.Text = "Three state CheckBox checked.";
}

private void HandleUnchecked(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    if (cb.Name == "cb1")
        text1.Text = "Two state CheckBox unchecked.";
    else
        text2.Text = "Three state CheckBox unchecked.";
}

private void HandleThirdState(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    text2.Text = "Three state CheckBox indeterminate.";
}
XAML
<!-- two state CheckBox -->
<CheckBox x:Name="cb1" Content="Two State CheckBox" 
          Checked="HandleCheck" Unchecked="HandleUnchecked" Margin="5" />
<TextBlock x:Name="text1" Margin="5" />

<!-- three state CheckBox -->
<CheckBox x:Name="cb2" Content="Three State CheckBox" 
    IsThreeState="True" Checked="HandleCheck" 
    Indeterminate="HandleThirdState" Unchecked="HandleUnchecked" Margin="5" />
<TextBlock x:Name="text2" Margin="5" />

Reference

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker