Control Collection

G

Guest

Hi All,

I have a GroupBox with 3 RadioButtons in it

There is a button (outside the GroupBox) which when clicked needs to find
the index of the RadioButton which is checked using a Select statement

How do I go about achieving this?

Thank you in advance,
 
H

Herfried K. Wagner [MVP]

SPAMCOP User said:
I have a GroupBox with 3 RadioButtons in it

There is a button (outside the GroupBox) which when clicked needs to find
the index of the RadioButton which is checked using a Select statement

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
///
 
J

James Hahn

Do you meant the index of the radiobutton in the groupbox controls
collection?

Private Function GetIndex() As Integer
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
If CType(GroupBox1.Controls(i), RadioButton).Checked Then
Return i
End If
End If
Next
End Function

then use the return value in the select statement.
 
G

Guest

Thank you

--
SPAMCOP User


Herfried K. Wagner said:
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

Hi James,

Example:

3 RadioButtons (rdoButton0, rdoButton1, rdoButton2) in a Groupbox

There is a button on the form which does either a server or a workstation
file copy depending on the what OS is picked up (example: Windows Vista
Business 32 Bit/XP Pro 64 Bit, Windows Server 2008 Standard/Datacenter...).
However the 3 radio buttons are for regions for proxy settings, adjusts the
hosts file etc. Therefore on button click I need to know if rdButton0, 1 or
2 was selected running the file copy/settings for that particulr region

Of course I can use the rdo click event to set an Integer value. Example:

Dim i As Integer

rdoButton0 Click Event
i = 0

rdoButton1

i = 1

and so on

I can then use a Select Case statement to handle the file copy/settings

With your code:

rdoButton0 returns 2, rdoButton1 returns 1 & rdoButton2 returns 0 which is
the exact opposite of what I need

Hope this has given you more of an insight

Thanks again for your reply
 
J

James Hahn

The index of the radiobuttons within the groupbox is assigned by the system
and is not under your control. That's why I queried what index you were
referring to, as it's not really useful (unless you have somehow already
worked out which index is which control). Also, you don;t know what might
cause these indeces to change.

If you are not referring to an index at all, but simply to some indicator as
to which control is currently selected, you can use the example code I
provided but return some other result. For instance, you can set the Tag
property of each Radiobutton and return that, or simply use the name. The
tag example would be:

Private Function GetIndex() As String
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
If CType(GroupBox1.Controls(i), RadioButton).Checked Then
Return CType(GroupBox1.Controls(i), RadioButton).Tag
End If
End If
Next
End Function

But this seems more complicated than necessary. Why use a Case statement?
Why not simply:

If RDButton1.Checked then
...
else if RDButton2.Checked then
etc

If you are committed to a Select statement than your original idea of
setting a variable to indicate the current selected radiobutton is simple
and effective.
 
S

Stewart Berman

If you really want to be able to use a select statement open each of the radio buttons' click event
and put:

Private Sub RadioButtonN_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButtonn.CheckedChanged
If (RadioButtonN.Checked) Then
GroupBox1.Tag = N
End If
End Sub

Where N is the number appended to the RadioRutton when you drag it into the GroupBox. You can then
use the GroupBox1.Tag property in your select statement.
 
G

Guest

Hi James,

I used the tag method but had to change it slightly because VS 2008 with
..NET Framework 3.5 moaned that the return value could be NULL

Private Function GetIndex() As String
Dim intTemp As Integer = 0
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
If CType(GroupBox1.Controls(i), RadioButton).Checked Then
intTemp = CType(GroupBox1.Controls(i), RadioButton).Tag
End If
End If
Next
Return intTemp
End Function

Thanks for your time & effort in replying!1

--
SPAMCOP User


James Hahn said:
The index of the radiobuttons within the groupbox is assigned by the
system and is not under your control. That's why I queried what index you
were referring to, as it's not really useful (unless you have somehow
already worked out which index is which control). Also, you don;t know
what might cause these indeces to change.

If you are not referring to an index at all, but simply to some indicator
as to which control is currently selected, you can use the example code I
provided but return some other result. For instance, you can set the Tag
property of each Radiobutton and return that, or simply use the name. The
tag example would be:

Private Function GetIndex() As String
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
If CType(GroupBox1.Controls(i), RadioButton).Checked Then
Return CType(GroupBox1.Controls(i), RadioButton).Tag
End If
End If
Next
End Function

But this seems more complicated than necessary. Why use a Case statement?
Why not simply:

If RDButton1.Checked then
...
else if RDButton2.Checked then
etc

If you are committed to a Select statement than your original idea of
setting a variable to indicate the current selected radiobutton is simple
and effective.
 
J

Jack Jackson

Hi James,

I used the tag method but had to change it slightly because VS 2008 with
.NET Framework 3.5 moaned that the return value could be NULL

Private Function GetIndex() As String
Dim intTemp As Integer = 0
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
If CType(GroupBox1.Controls(i), RadioButton).Checked Then
intTemp = CType(GroupBox1.Controls(i), RadioButton).Tag
End If
End If
Next
Return intTemp
End Function

Thanks for your time & effort in replying!1

What the error is really telling you is that you have a way out of the
function (if the For loop doesn't find a match) that doesn't
explicitly return a value:

"Function 'GetIndex' doesn't return a value on all code paths. A null
reference exception could occur at turn time when the result is used."

You could have fixed the error by putting a 'Return ""' at then end of
the routine, rather than introducing unnecessary conversions between
Strings and Integers.

A bigger problem is that you are not using Option Strict On. Your
final code has an implicit conversion from Object to Integer, and
another from Integer to String, which is inefficient. Assuming the
Tag fields contain strings, I would do it this way:

Private Function GetIndex() As String
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
Dim rb As RadioButton = DirectCast(GroupBox1.Controls(i),
RadioButton)
If rb.Checked Then
Return CType(rb.Tag, String)
End If
End If
Next
Return "" ' Or Return Nothing
End Function

If you really want an Integer value for which button is checked, then
you should store Integers in the Tag properties, change the function
to return an Integer, and CType the Tag field to Integer. That way
you won't be doing unnecessary conversions from String to Integer in
your code.
 
J

James Hahn

I'm glad the suggestion was helpful, but it still seems much more complex
than necessary.
 
C

Cor Ligthert[MVP]

Hi,

A variation on the same
\\\
For Each rb As Control In GroupBox1.Controls
if TypeOf (rb) Is RadioButton Then
If DirectCast(rb, RadioButton).Checked Then
Return rb.Tag.ToString
End If
End If
Next
Return ""
///

Cor

SPAMCOP User said:
Hi James,

I used the tag method but had to change it slightly because VS 2008 with
.NET Framework 3.5 moaned that the return value could be NULL

Private Function GetIndex() As String
Dim intTemp As Integer = 0
For i As Integer = 0 To GroupBox1.Controls.Count - 1
If TypeOf (GroupBox1.Controls(i)) Is RadioButton Then
If CType(GroupBox1.Controls(i), RadioButton).Checked Then
intTemp = CType(GroupBox1.Controls(i), RadioButton).Tag
End If
End If
Next
Return intTemp
End Function

Thanks for your time & effort in replying!1
 

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