Home All Groups Group Topic Archive Search About

Radio Button Grouping in vb/VS 2005

Author
3 Mar 2006 12:00 AM
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

Author
3 Mar 2006 1:34 AM
Homer J Simpson
"Thomas Homan" <tho***@co.gila.az.us> wrote in message
news:eeQKwVlPGHA.4956@TK2MSFTNGP09.phx.gbl...

> 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
Author
3 Mar 2006 2:59 PM
Thomas Homan
That works, thanks!
"Homer J Simpson" <nob***@nowhere.com> wrote in message
news:T2NNf.10801$vC4.8888@clgrps12...
Show quoteHide quote
>
> "Thomas Homan" <tho***@co.gila.az.us> wrote in message
> news:eeQKwVlPGHA.4956@TK2MSFTNGP09.phx.gbl...
>
>> 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
>
>
Author
3 Mar 2006 4:03 AM
Virgil
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
Author
3 Mar 2006 3:02 PM
Thomas Homan
Thanks virgil, that fits my needs exactly

Tom
Show quoteHide quote
"Virgil" <vschle***@trilliumteam.com> wrote in message
news:1141358622.986081.52580@v46g2000cwv.googlegroups.com...
> 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
>