RadioButton checked event handler : RadioButton « Windows Presentation Foundation « VB.Net






RadioButton checked event handler

RadioButton checked event handler
   
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" SizeToContent="Height" Width="300">
    <Grid Name="grid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0" BorderBrush="Gray" BorderThickness="1" />
        <Border Grid.Column="1" BorderBrush="Gray" BorderThickness="1" />
        <StackPanel Grid.Column="0" HorizontalAlignment="Center" Margin="5" Name="spLeftContainer">
            <TextBlock FontSize="16" Text="Radio Group 1" />
            <RadioButton Content="Radio Button 1A" GroupName="Group1" IsChecked="True" Margin="5" Name="rbnOneA" />
            <RadioButton Content="Radio Button 1B" GroupName="Group1" Margin="5" Name="rbnOneB" />
            <RadioButton Content="Radio Button 1C" GroupName="Group1" Margin="5" Name="rbnOneC" />
            <Separator/>
            <TextBlock FontSize="16" Text="Radio Group 2" />
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2A" IsChecked="True" 
                         Margin="5" Name="rbnTwoA" />
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2B" Margin="5" Name="rbnTwoB"/>
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2C" Margin="5" Name="rbnTwoC"/>
        </StackPanel>
        <StackPanel Grid.Column="1" HorizontalAlignment="Center" Margin="5" Name="spRightContainer">
            <TextBlock FontSize="16" Text="Radio Group 1" />
            <RadioButton Content="Radio Button 1D" GroupName="Group1" Margin="5" Name="rbnOneD" />
            <RadioButton Content="Radio Button 1E" GroupName="Group1" Margin="5" Name="rbnOneE" />
        </StackPanel>
        <Button Content="Show Group1 Selection" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center" 
                Margin="10" MaxHeight="25" Click="Button_Click" />
    </Grid>
</Window>

//File:Window.xaml.vb
Imports System
Imports System.Linq
Imports System.Windows
Imports System.Windows.Controls

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub
    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
      Dim radioButton As RadioButton = Nothing
      radioButton = GetCheckedRadioButton(spLeftContainer.Children, "Group1")
      If radioButton Is Nothing Then
        radioButton = GetCheckedRadioButton(spRightContainer.Children, "Group1")
      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
  End Class
End Namespace

   
    
    
  








Related examples in the same category

1.Grouping radio buttons by parentGrouping radio buttons by parent
2.Grouping radio buttons by nameGrouping radio buttons by name
3.CheckRadio GroupCheckRadio Group
4.Image RadioButtonImage RadioButton
5.RadioButton click eventRadioButton click event
6.Use RadioButton to control TextBox alignmentUse RadioButton to control TextBox alignment