WPF Handle CheckBox Unchecked Event in VB.NET
In this article you will learn that how you can handle CheckBox unchecked Event.
Introduction
Here in this article we are discussing that how to handle Checkbox unchecked event. The implementation of example needs StackPanel, CheckBox, TextBlock, ListBox and a Button control. The CheckBox which are shown in StackPanel will be checked or unchecked,When you click on Button checked CheckBox will display in TextBlock with its name. You can handle CheckBox unchecked event with a Small coding and controls.
Getting Started
- Simply create a new WPF application.
- Drag a StackPanel, TextBlock, ListBox, CheckBox and Button control on MainWindow. Your Window Will look below.
- After adding Controls your MainWindow.xaml page will look like below.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="248">
<StackPanel Name="panel">
<CheckBox Content="First CheckBox"
IsChecked="False" Margin="2" Name="checkbox1"
Unchecked="CheckBox_Unchecked"/>
<CheckBox Content="Second CheckBox"
IsChecked="True" Margin="2" Name="checkbox2"
Unchecked="CheckBox_Unchecked"/>
<Button Content="Show Selected CheckBox" Margin="5" MaxWidth="100"
Click="Button_Click" />
<TextBlock FontWeight="Bold" Text="CheckBox Selection:" />
<ListBox Margin="5" MinHeight="2cm" Name="listbox" />
</StackPanel>
</Window>
- Then add the below code in code Behind file.
Private Sub btnselect_Click(sender As Object, e As RoutedEventArgs)
listbox.Items.Clear()
For Each checkbox As CheckBox In panel.Children.OfType(Of CheckBox)().Where(Function(cb)
cb.IsChecked = True)
listbox.Items.Add(checkbox.Name)
Next
End Sub
Private Sub chkbox_Unchecked(sender As Object, e As RoutedEventArgs)
If Not IsInitialized Then
Return
End If
Dim chkbox As CheckBox = TryCast(e.OriginalSource, CheckBox)
If chkbox IsNot Nothing Then
MessageBox.Show(chkbox.Name & " is unchecked.", Title)
End If
End Sub
- Now run your application.
Output:-
Summary
In this article you learned that how to handle CheckBox unchecked event.