Radio Button Grouping in vb/VS 2005

  • Thread starter Thread starter Thomas Homan
  • Start date Start date
T

Thomas Homan

All,

Is there an easy way to interate through a group of radio buttons (9 of 'em)
contained within a group box in VS2005. I really don't want to have to
evaluate each one...

TIA!

Tom
 
Is there an easy way to interate through a group of radio buttons (9 of
'em) contained within a group box in VS2005. I really don't want to have
to evaluate each one...

You can write one sub/function to handle them all. Set the .Tag value to the
Button number.

Here is a horrible example:

Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, _
RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, _
RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, _
RadioButton6.CheckedChanged, RadioButton7.CheckedChanged, _
RadioButton8.CheckedChanged, RadioButton9.CheckedChanged
Debug.Print(sender.tag)
RadioButton1.BackColor = Drawing.SystemColors.Control
RadioButton2.BackColor = Drawing.SystemColors.Control
RadioButton3.BackColor = Drawing.SystemColors.Control
RadioButton4.BackColor = Drawing.SystemColors.Control
RadioButton5.BackColor = Drawing.SystemColors.Control
RadioButton6.BackColor = Drawing.SystemColors.Control
RadioButton7.BackColor = Drawing.SystemColors.Control
RadioButton8.BackColor = Drawing.SystemColors.Control
RadioButton9.BackColor = Drawing.SystemColors.Control
Select Case sender.tag
Case 1
RadioButton1.BackColor = Color.Red
Case 2
RadioButton2.BackColor = Color.Red
Case 3
RadioButton3.BackColor = Color.Red
Case 4
RadioButton4.BackColor = Color.Red
Case 5
RadioButton5.BackColor = Color.Red
Case 6
RadioButton6.BackColor = Color.Red
Case 7
RadioButton7.BackColor = Color.Red
Case 8
RadioButton8.BackColor = Color.Red
Case 9
RadioButton9.BackColor = Color.Red
End Select
End Sub
 
Below are a few functions I put together to identify the selected
radiobutton in a group box. The first function is a button click event
that kicked off the test. The second is the one that lookes for the
radiobutton. I hope this helps.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Dim ARadioButton As RadioButton
ARadioButton = GetCheckedRadioButton(Me.GroupBox1)
If ARadioButton Is Nothing Then
MessageBox.Show("Please select an option.")
Else
MessageBox.Show(ARadioButton.Text)
End If

End Sub

Protected Function GetCheckedRadioButton(ByVal Parent As Control)
As RadioButton
Dim AControl As Control
Dim ARadioButton As RadioButton

For Each AControl In Parent.Controls
If AControl.GetType() Is GetType(RadioButton) Then
ARadioButton = DirectCast(AControl, RadioButton)
If ARadioButton.Checked Then Return ARadioButton
End If
Next

Return Nothing
End Function
 
That works, thanks!
Homer J Simpson said:
You can write one sub/function to handle them all. Set the .Tag value to
the Button number.

Here is a horrible example:

Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, _
RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, _
RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, _
RadioButton6.CheckedChanged, RadioButton7.CheckedChanged, _
RadioButton8.CheckedChanged, RadioButton9.CheckedChanged
Debug.Print(sender.tag)
RadioButton1.BackColor = Drawing.SystemColors.Control
RadioButton2.BackColor = Drawing.SystemColors.Control
RadioButton3.BackColor = Drawing.SystemColors.Control
RadioButton4.BackColor = Drawing.SystemColors.Control
RadioButton5.BackColor = Drawing.SystemColors.Control
RadioButton6.BackColor = Drawing.SystemColors.Control
RadioButton7.BackColor = Drawing.SystemColors.Control
RadioButton8.BackColor = Drawing.SystemColors.Control
RadioButton9.BackColor = Drawing.SystemColors.Control
Select Case sender.tag
Case 1
RadioButton1.BackColor = Color.Red
Case 2
RadioButton2.BackColor = Color.Red
Case 3
RadioButton3.BackColor = Color.Red
Case 4
RadioButton4.BackColor = Color.Red
Case 5
RadioButton5.BackColor = Color.Red
Case 6
RadioButton6.BackColor = Color.Red
Case 7
RadioButton7.BackColor = Color.Red
Case 8
RadioButton8.BackColor = Color.Red
Case 9
RadioButton9.BackColor = Color.Red
End Select
End Sub
 
Back
Top