groupbox and radiobutton relationship - which radiobutton is check

G

Guest

Hello,

GroupBox1 contains 3 radiobuttons. Is there a builtin way - a Container
property - in the groupbox control to determine which radiobutton is checked
- if any? I did not see anything like GroupBox1.Value, but in pseudocode I
am thinking something like

Console.WriteLine(GroupBox1.Value.ToString)

I tried

Console.WriteLine(GroupBox1.Container.Components.Count.ToString)

but got an Object Not Set error.

Am I limited to looping through an array of radiobuttons to see which one is
checked?

Dim arrRads() as radioButton
arrRads = New RadioButton(){rad1, rad2, rad3}
....
For Each rad In arrRads
If rad.Checked.Equals(True) Then Console.WriteLine(rad.Name " = True")
Next

What is the correct way - most efficient way to determine - return the
checked radiobutton in the GroupBox? Say you have GroupBox1 and GroupBox2,
they each contain 3 radiobuttons. Are the 3 radiobuttons in GroupBox1 part
of GroupBox1 and the 3 in GroupBox2 part of GroupBox2 or is each radiobutton
an independent entity to be treated independently of the Groupbox it is
contained in?

Thanks,
Rich
 
H

Herfried K. Wagner [MVP]

Rich said:
GroupBox1 contains 3 radiobuttons. Is there a builtin way - a Container
property - in the groupbox control to determine which radiobutton is
checked
- if any? I did not see anything like GroupBox1.Value, but in pseudocode
I
am thinking something like

No, there is no such property, but you can easily maintain it on your own.
To do so, add a common event handler to all the radio buttons which belong
to a group:

\\\
Private m_Group1SelectedRadioButton As RadioButton

Private Sub RadioButtonGroup1_CheckedChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles _
RadioButton1.CheckedChanged, _
RadioButton2.CheckedChanged, _
RadioButton3.CheckedChanged

Dim SourceControl As RadioButton = DirectCast(sender, RadioButton)
If SourceControl.Checked Then
m_Group1SelectedRadioButton = SourceControl
End If
End Sub
///
 
G

Guest

Thank you. That was just the tip that I needed. I will use Event handlers
for each groupbox. I was actually thinking of using array.IndexOf for a 2
dimensional array where I would have an array of arrays of radiobuttons
within each groupbox. But array.IndexOf only works with 1 dimensional
arrays. So your suggestion solves my problem.

Thank,
Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top