By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,440 Members | 1,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,440 IT Pros & Developers. It's quick & easy.

determine which radiobutton is checked in group of radiobuttons?

P: n/a
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
Jan 11 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Jan 11, 9:37 am, Rich <R...@discussions.microsoft.comwrote:
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
Jan 11 '08 #2

P: n/a
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:
On Jan 11, 9:37 am, Rich <R...@discussions.microsoft.comwrote:
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
Jan 11 '08 #3

P: n/a
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:
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:
On Jan 11, 9:37 am, Rich <R...@discussions.microsoft.comwrote:
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
Jan 11 '08 #4

P: n/a
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

"Rich" <Ri**@discussions.microsoft.comwrote in message
news:1B**********************************@microsof t.com...
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

Jan 11 '08 #5

P: n/a
"Rich" <Ri**@discussions.microsoft.comschrieb:
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/>

Jan 12 '08 #6

P: n/a
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.
Sep 4 '08 #7

P: n/a
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:20************@sol.dk...
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.

Sep 4 '08 #8

P: n/a
On Sep 4, 1:41*pm, Eske Rahn wrote:
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.
Sep 4 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.