|
|
|
|
|
Home
»
WPF
»
How to handle RadioButton Checked Event in WPF using VB.NET
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
1949
|
|
Downloads :
|
5
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
Introduction
Here in this article I am explaining that how to Handle RadioButton checked Event in WPF. RadioButton is used for the selection purpose when we want to make a choice from number of items. We can check one RadioButton at a time. In the example we are showing the RadioButton Groups and when we checked any item of group one and press the Button a MessageBox will display with the checked Item name. when we checked the items of Group 2 than on Checked Event a MessageBox will display with the checked item name. The implementation needs a Grid, StackPanel, RadioButton and a Button control.
Getting Started
- Simply create a new WPF application
- Drag a Grid, StackPanel, RadioButton and a Button control on MainWindow. Your Window will look like below.

- 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>
- Then 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

 
 

Summary
In this article you learned that how to handle RadioButton Checked Event.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|