Connecting Tech Pros Worldwide Forums | Help | Site Map

determine which radiobutton is checked in group of radiobuttons?

=?Utf-8?B?UmljaA==?=
Guest
 
Posts: n/a
#1: Jan 11 '08
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).Ro ws.Count.ToString)
End Sub

vs

Private Sub someControl_Click(...) handles...
If radio1.Checked.Equals(True) then
console.WriteLine(dataset1.Tables(1).Rows.Count.To String)
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

Jeremy Cowles
Guest
 
Posts: n/a
#2: Jan 11 '08

re: determine which radiobutton is checked in group of radiobuttons?


On Jan 11, 9:37 am, Rich <R...@discussions.microsoft.comwrote:
Quote:
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
=?Utf-8?B?UmljaA==?=
Guest
 
Posts: n/a
#3: Jan 11 '08

re: determine which radiobutton is checked in group of radiobuttons?


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?

"Jeremy Cowles" wrote:
Quote:
On Jan 11, 9:37 am, Rich <R...@discussions.microsoft.comwrote:
Quote:
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
>
=?Utf-8?B?UmljaA==?=
Guest
 
Posts: n/a
#4: Jan 11 '08

re: determine which radiobutton is checked in group of radiobuttons?


Here is something that sort of works

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


"Rich" wrote:
Quote:
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?
>
"Jeremy Cowles" wrote:
>
Quote:
On Jan 11, 9:37 am, Rich <R...@discussions.microsoft.comwrote:
Quote:
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
md
Guest
 
Posts: n/a
#5: Jan 11 '08

re: determine which radiobutton is checked in group of radiobuttons?


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

"Rich" <Rich@discussions.microsoft.comwrote in message
news:1B6B13A8-7DB5-48D6-9A1E-00C5DAD8C6E2@microsoft.com...
Quote:
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).Ro ws.Count.ToString)
End Sub
>
vs
>
Private Sub someControl_Click(...) handles...
If radio1.Checked.Equals(True) then
console.WriteLine(dataset1.Tables(1).Rows.Count.To String)
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

Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Jan 12 '08

re: determine which radiobutton is checked in group of radiobuttons?


"Rich" <Rich@discussions.microsoft.comschrieb:
Quote:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Eske Rahn
Guest
 
Posts: n/a
#7: Sep 4 '08

re: determine which radiobutton is checked in group of radiobuttons?


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.
Cor Ligthert [MVP]
Guest
 
Posts: n/a
#8: Sep 4 '08

re: determine which radiobutton is checked in group of radiobuttons?


Eske

Instead of writting it here, see what is in this tip about your question

http://www.vb-tips.com/DynamicControl.aspx

Cor

<Eske Rahnschreef in bericht news:2008946414junk@sol.dk...
Quote:
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.

kimiraikkonen
Guest
 
Posts: n/a
#9: Sep 4 '08

re: determine which radiobutton is checked in group of radiobuttons?


On Sep 4, 1:41*pm, Eske Rahn wrote:
Quote:
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.
Closed Thread