WPF Handle RadioButton Checked Event in VB.NET

In this article you will learn that how you can handle RadioButton checked Event.
  • 5394
Download Files:
 

Introduction

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.

    radio1.gif
     
  • 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="305" Width="424" Background="Chocolate">
        <Grid Name="grid">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <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" />
                <Separator/>
                <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"
    Name="rbnTwoB"/>
                <RadioButton Checked="RadioButton_Checked" GroupName="grp2" Content="SilverLight2" Margin="5"
    Name="rbnTwoC"/>
            </StackPanel>
            <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" />
            </StackPanel>
            <Button Content="Display grp1 Selection" Grid.ColumnSpan="2" Grid.Row="1"
    HorizontalAlignment="Center"
                    Margin="10" MaxHeight="25" Click="btn_Click" />
        </Grid>
    </
    Window>
     
  • 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
            RadioButton
            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
                Return
            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.

Output

radio2.gif

radio3.gif

radio4.gif

radio5.gif

radio6.gif

radio7.gif

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.