In this article we will learn how to use CheckBox control in Silverlight.
CheckBox control
The CheckBox control is used to display a check box. The CheckBox control gives us an option to select true/false. A checkbox is clicked to select and clicked again to deselect some option. When a checkbox is selected, a check (a tick mark) appears indicating a selection.
Properties:
Figure 1.
Content - show the text shown with the CheckBox.
IsChecked: Specifies whether the check box is checked or not.
CheckBox event:
Private Sub CheckBox1_Checked(ByVal sender As System.Object, ByVal e AsSystem.Windows.RoutedEventArgs)
Handles CheckBox1.Checked
End Sub
For example:
Drag two CheckBoxes, a TextBox and a Button control on to the form. Set the Text property for the CheckBoxes as Male and Female. The following code will display the text of the Checkbox that is checked in the textbox when the button is clicked. Form looks like this:
Figure 2.
XAML code:
<UserControl x:Class="SilverlightApplication9.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" Height="432" Width="609">
<Canvas Height="344" Name="Canvas1" Width="372" Cursor="None">
<CheckBox Canvas.Left="14" Canvas.Top="16" Height="23" Name="CheckBox1"Content="Male" FontSize="16" Width="69" />
<TextBox Canvas.Left="97" Canvas.Top="16" Height="23" Name="TextBox1" Width="120" />
<Button Canvas.Left="123" Canvas.Top="55" Content="Button" Height="23" Name="Button1"Width="75" />
<CheckBox Canvas.Left="12" Canvas.Top="55" Content="Female" Height="16"Name="CheckBox2" FontSize="14" />
</Canvas>
</UserControl>
Now double click on the button control and add the following code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.Windows.RoutedEventArgs) Handles Button1.Click
If CheckBox1.IsChecked Then
TextBox1.Text = "Your Gender: mail"
ElseIf CheckBox2.IsChecked Then
TextBox1.Text = "Your gender: Femail"
End If
End Sub
Now save and run the application.
Figure 3.