determine which radiobutton is checked in group of radiobuttons?

R

Rich

If you enclose a group of radiobuttons (option buttons in MS Access) in an
option group control (a frame control) in Access -- the frame control will
return the index of the option button that is checked.

In VB.Net if I enclose a group of radiobuttons in a groupbox control - I get
the single checked radiobutton behavior, but the groupbox does not seem to
return the index of the checked radiobutton. Is there a way to get the
groupbox control to return the index of the checked radiobutton? Or is there
some control structure that could do this?

My workaround for now is to add a handler to each radiobutton which will set
its index value to a global variable - call it global_Int. But this seems
kind of kludgy. What is the most efficient way to return the index of the
checked radio button in a group of radiobuttons?

Usage - pseudocode here
Private Sub someControl_Click(...) handles...
console.WriteLine(dataset1.Tables(Frame1.Value).Rows.Count.ToString)
End Sub

vs

Private Sub someControl_Click(...) handles...
If radio1.Checked.Equals(True) then
console.WriteLine(dataset1.Tables(1).Rows.Count.ToString)
ElseIf radio2.Checked.Equal(True) then
....
End Sub

or my current method

Private Sub someControl_Click(...) handles...
console.WriteLine(dataset1.Tables(global_Int).Rows.Count.ToString)
End Sub


Thanks,
Rich
 
J

Jeremy Cowles

If you enclose a group of radiobuttons (option buttons in MS Access) in an
option group control (a frame control) in Access -- the frame control will
return the index of the option button that is checked.

In VB.Net if I enclose a group of radiobuttons in a groupbox control - I get
the single checked radiobutton behavior, but the groupbox does not seem to
return the index of the checked radiobutton. Is there a way to get the
groupbox control to return the index of the checked radiobutton? Or is there
some control structure that could do this?

Rich

I think the easiest way to replicate indexed controls is to have one
event handler for all controls in the array. For example, if you have
two radio buttons, RadioButton1 and RadioButton2, the following cold
should accomplish what you want:

Private CheckedRadio As RadioButton
Private Sub Options_CheckedChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles RadioButton1.CheckedChanged, _
RadioButton2.CheckedChanged

Dim c As RadioButton = CType(sender, RadioButton)
If c.Checked Then
CheckedRadio = c
End If
End Sub

Now after a radio button is checked, the variable CheckedRadio will be
a reference to the checked control.

Is that what you were trying to do?

-
Jeremy
 
R

Rich

Thank you for your reply, but that is sort of what I am already doing in my
workaround. Here is what I am currently doing

dim arrRads() as RadioButton = New RadioButton(){rad1, rad2, rad3}

For each rad As RadioButton In arrRads
AddHandler rad.Click, AddressOf CheckChanged
Next

Private Sub CheckChanged(...)
Dim rad As RadioButton = Ctype(Sender, RadioButton)
If rad.Name.Equal("rad1") Then global_Int = 1
....
End Sub

Your method is a little bit better, but I still have to get the index of the
radiobtn. Is there a way I could do your thing to get the index from my
array above?

GetIndext(arrRads(rad)) -- arrRads(rad).IndexOf


With your suggestion I am getting a little bit closer to what I want. I
think the trick will be in the IndexOf property. How to apply it?
 
R

Rich

Here is something that sort of works

Private function radChecked(...)
dim rad as radiobutton = ctype(sender, radiobutton)
return Array.IndexOf(arrayRads, rad)
End Function
 
M

md

One way I have done it in the past is to use

Select Case True
Case RadioButton1.Checked
Case RadioButton2.Checked
Case Else
End Select

Although the event handler way is probably a bit more the ".Net way" to do
it.

Matt
 
H

Herfried K. Wagner [MVP]

Rich said:
If you enclose a group of radiobuttons (option buttons in MS Access) in an
option group control (a frame control) in Access -- the frame control will
return the index of the option button that is checked.

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

Eske Rahn

Private Sub RB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
RBa.CheckedChanged, RBb.CheckedChanged, RBc.CheckedChanged, RBd.CheckedChanged
If sender.checked Then
Dim ci as Integer = Me.Controls.IndexOf(sender) - Me.Controls.IndexOf(RBa)
DoYourJob(ci)
End If
End Sub



Assuming the buttons are added like
Me.Controls.Add(Me.RBa)
Me.Controls.Add(Me.RBb)
Me.Controls.Add(Me.RBc)
Me.Controls.Add(Me.RBd)

in the " Windows Form Designer generated code " or manually.
 
K

kimiraikkonen

Private Sub RB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
      RBa.CheckedChanged, RBb.CheckedChanged, RBc.CheckedChanged, RBd.CheckedChanged
    If sender.checked Then
      Dim ci as Integer = Me.Controls.IndexOf(sender) - Me.Controls.IndexOf(RBa)
      DoYourJob(ci)
    End If
  End Sub

Assuming the buttons are added like
    Me.Controls.Add(Me.RBa)
    Me.Controls.Add(Me.RBb)
    Me.Controls.Add(Me.RBc)
    Me.Controls.Add(Me.RBd)

in the " Windows Form Designer generated code " or manually.

Note that the thread remains from 11th of January which you answered.
 

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