Click here to Skip to main content
12,406,098 members (73,295 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

15.5K views
3 bookmarked
Posted

Determine selected RadioButton in a Group of RadioButtons

, 5 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This code permits a developer to determine if a radio button is selected in a container such as a panel or GroupBox using syntax similar to TryParse.Requires Requires Framework 3.5 or higherExamplePrivate Sub Button1_Click() Handles Button1.Click Dim SelectedButton As New...
This code permits a developer to determine if a radio button is selected in a container such as a panel or GroupBox using syntax similar to TryParse.

Requires Requires Framework 3.5 or higher


Example
Private Sub Button1_Click() Handles Button1.Click
    Dim SelectedButton As New RadioButton
    If GroupBox1.GetSelectedRadioButton(SelectedButton) Then
        MsgBox(SelectedButton.Name)
    Else
        MsgBox("Please make a selection")
    End If
End Sub

Extension to be placed in a code module
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function GetSelectedRadioButton(ByVal container As Control, _
                                       ByRef Button As RadioButton) As Boolean
    Dim Result As Boolean = False
 
    Dim RadioCollection = From Control In container.Controls _
                          Where TypeOf Control Is RadioButton Select Control
 
    If Not RadioCollection Is Nothing Then
        Dim CheckedButton = (From Item In RadioCollection.Cast(Of RadioButton)() _
                             Where Item.Checked).DefaultIfEmpty(Button).First
 
        If Not CheckedButton.Name.Equals(Button.Name) Then
            Button = CheckedButton
            Result = True
        End If
    End If
 
    Return Result
 
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

karenpayne
Instructor / Trainer
United States United States
Microsoft MVP, Trouble shooter, component builder, and architect using Microsoft Visual Studio 2012/2013. Prior to VS .NET I developed solutions using Borland Delphi and before Delphi CA Clipper. I enjoy assisting others, sharing knowledge and learning from others. Outside of IT my passions are playing guitar and teaching self-defense. Currently a moderator for Microsoft Social forums in C#, VB.NET and Data binding forums. Lastly a MCC (Microsoft Community Contributor).

You may also be interested in...

Comments and Discussions

 
GeneralNice solution Pin
Johnny J.4-Mar-10 20:15
memberJohnny J.4-Mar-10 20:15 
GeneralRe: Nice solution Pin
GlimmerMan5-Mar-10 4:17
memberGlimmerMan5-Mar-10 4:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160726.1 | Last Updated 5 Mar 2010
Article Copyright 2010 by karenpayne
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid