I have a lot of radio buttons in a groupbox. Normally I will check each radio button individually using If radiobutton1.Checked = True Then
.
But I think
If you add them (Load event for instance) to a List you could use LINQ:
Dim checkedRadioButton as RadioButton
checkedRadioButton =
radioButtonList.FirstOrDefault(Function(radioButton) radioButton.Checked))
This should be OK because there is a single one checked at the most.
EDIT Even better: just query the Controls collection of the GroupBox:
Dim checkedRadioButton as RadioButton
checkedRadioButton =
groupBox.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(radioButton) radioButton.Checked))
Be aware that this will cause problems if there are no RadioButtons in the Groupbox!
Here is a test program with a groupbox with four radio buttons.
Public Class Form1
Private Sub Form1_Shown(sender As Object, _
e As System.EventArgs) Handles Me.Shown
RadioButton1.Tag = New Action(AddressOf rb1Action)
RadioButton2.Tag = New Action(AddressOf rb2Action)
RadioButton3.Tag = New Action(AddressOf rb3Action)
RadioButton4.Tag = New Action(AddressOf rb4Action)
End Sub
Private Sub rb1Action()
Debug.WriteLine("1 " & RadioButton1.Checked)
End Sub
Private Sub rb2Action()
Debug.WriteLine("2 " & RadioButton2.Checked)
End Sub
Private Sub rb3Action()
Debug.WriteLine("3 " & RadioButton3.Checked)
End Sub
Private Sub rb4Action()
Debug.WriteLine("4 " & RadioButton4.Checked)
End Sub
Private Sub RadioButton_CheckedChanged(sender As System.Object, _
e As System.EventArgs) Handles _
RadioButton1.CheckedChanged, _
RadioButton2.CheckedChanged, _
RadioButton3.CheckedChanged, _
RadioButton4.CheckedChanged
Dim aRadioButton As RadioButton = DirectCast(sender, RadioButton)
If aRadioButton.Checked Then
Dim rbAct As Action = DirectCast(aRadioButton.Tag, Action)
rbAct.Invoke()
End If
End Sub
End Class
'returns which radio button is selected within GroupBox passed
Private Function WhatRadioIsSelected(ByVal grp As GroupBox) As String
Dim rbtn As RadioButton
Dim rbtnName As String = String.Empty
Try
Dim ctl As Control
For Each ctl In grp.Controls
If TypeOf ctl Is RadioButton Then
rbtn = DirectCast(ctl, RadioButton)
If rbtn.Checked Then
rbtnName = rbtn.Name
Exit For
End If
End If
Next
Catch ex As Exception
Dim stackframe As New Diagnostics.StackFrame(1)
Throw New Exception("An error occurred in routine, '" & stackframe.GetMethod.ReflectedType.Name & "." & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'." & Environment.NewLine & " Message was: '" & ex.Message & "'")
End Try
Return rbtnName
End Function
There are 3 RadioButtons: RadioButton1, RadioButton2 y RadioButton3
'A handler for the click event of the 3 buttons is created
Private Sub Radios_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
Dim rb As RadioButton
rb = sender
MsgBox(rb.Name) 'Displays the name of the selected control or that he was made the click
End Sub
You can run a foreach loop to iterate with the scans of controls RadioButton inside the GroupBox, so using the Control, see example below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'RadioButton checked
Dim ceckedRadioButton As Integer = 0
'Total RadioButton on GroupBox
Dim totalRadioButton As Integer = 0
'Iteration and check RadioButton selected
For Each myControl As RadioButton In Me.GroupBox1.Controls.OfType(Of RadioButton)()
'If RadioButton is checked
If myControl.Checked Then
'increases variable ceckedRadioButton
ceckedRadioButton += 1
End If
'increases variable totalRadioButton
totalRadioButton += 1
Next
If ceckedRadioButton > 0 Then
'RadioButon show how many are selected
MessageBox.Show("Were selected" & " " & ceckedRadioButton.ToString & " " & "RadioButton on" & " " & totalRadioButton.ToString)
Else
'No selected RadioButton
MessageBox.Show("No selected RadioButton")
End If
End Sub
Bye
Private Sub BTN_OK_Click(sender As Object, e As EventArgs) Handles BTN_OK.Click
For Each Ctrl In GroupBox1.Controls
If Ctrl.checked Then MsgBox(Ctrl.text) 'for show select RadioButton Check
Next