WPF Handle RadioButton Checked Event in VB.NET

In this article you will learn that how you can handle RadioButton checked Event.
  • 5824


Here in this article we are discussing that how to Handle RadioButton checked Event in WPF. RadioButton is used for  selection purpose, when you want to make a choice from number of items. You can check one RadioButton at a time. In the given example we are showing RadioButton Groups and when you checked any item of group one and press Button a MessageBox will display with the checked Item name. When you checked the items of Group 2 then on Checked Event a MessageBox will display with the checked item name. The implementation of example needs a Grid, StackPanel, RadioButton and a Button control.

Getting Started

  • Simply create a new WPF application. 
  • Drag a Grid, StackPanel, RadioButton and Button control on MainWindow. Your Window will look like below.

  • Your MainWindow.xaml page will look like below.

    <Window x:Class="MainWindow"
        Title="MainWindow" Height="305" Width="424" Background="Chocolate">
        <Grid Name="grid">
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            <Border Grid.Column="0" BorderBrush="Black" BorderThickness="1" />
            <Border Grid.Column="1" BorderBrush="Black"  BorderThickness="1" />
            <StackPanel Grid.Column="0" HorizontalAlignment="Center" Margin="5" Name="spLeftContainer">
                <TextBlock FontSize="16" Text="Radio Buttons in Group 1" />
                <RadioButton Content="vb1" GroupName="grp1" IsChecked="True" Margin="5" Name="rbnOneA" />
                <RadioButton Content="c#1" GroupName="grp1" Margin="5" Name="rbnOneB" />
                <RadioButton Content="ASP.NET1" GroupName="grp1" Margin="5" Name="rbnOneC" />
                <TextBlock FontSize="16" Text="Radio Buttons in Group 2" />
                <RadioButton Checked="RadioButton_Checked" GroupName="grp2" Content="WPF2" IsChecked="True"
                             Margin="5" Name="rbnTwoA" />
                <RadioButton Checked="RadioButton_Checked" GroupName="grp2" Content="SharePoint2" Margin="5"
                <RadioButton Checked="RadioButton_Checked" GroupName="grp2" Content="SilverLight2" Margin="5"
            <StackPanel Grid.Column="1" HorizontalAlignment="Center" Margin="5" Name="spRightContainer">
                <TextBlock FontSize="16" Text="Radio Buttons in Group 1" />
                <RadioButton Content="WCF1" GroupName="grp1" Margin="5" Name="rbnOneD" />
                <RadioButton Content="Crystal Report1" GroupName="grp1" Margin="5" Name="rbnOneE" />
            <Button Content="Display grp1 Selection" Grid.ColumnSpan="2" Grid.Row="1"
                    Margin="10" MaxHeight="25" Click="btn_Click" />
  • Than add the below code in Code Behind File.

        Private Sub btn_Click(sender As Object, e As RoutedEventArgs)
            Dim radioButton As RadioButton = Nothing
            radioButton = GetCheckedRadioButton(spLeftContainer.Children, "grp1")
            If radioButton Is Nothing Then
                radioButton = GetCheckedRadioButton(spRightContainer.Children, "grp1")
            End If
            MessageBox.Show(Convert.ToString(radioButton.Content) & " checked.", Title)
        End Sub
        Private Function GetCheckedRadioButton(children As UIElementCollection, groupName As [String]) As
            Return children.OfType(Of RadioButton)().FirstOrDefault(Function(rb) rb.IsChecked = True AndAlso
            rb.GroupName = groupName)
        End Function
        Private Sub RadioButton_Checked(sender As Object, e As RoutedEventArgs)
            If Not Me.IsInitialized Then
            End If
            Dim radioButton As RadioButton = TryCast(e.OriginalSource, RadioButton
            If radioButton IsNot Nothing Then
                MessageBox.Show(Convert.ToString(radioButton.Content) & " checked.", Title)
            End If
        End Sub

  • Now run your application.








In this article you learned that how to handle RadioButton Checked Event.


More Articles

© 2020 DotNetHeaven. All rights reserved.