array of radiobuttons - which rad is checked - without looping?

  • Thread starter Thread starter Rich P
  • Start date Start date
R

Rich P

The following 5 radio buttons are contained in a groupbox on a form

Dim arrRad() As RadioButton

arrRad = New RadioButton(){rad1, rad2, rad3, rad4, rad5}

Is there a way to select which of the 5 radio buttons in the array (in
the groupbox) is the checked radiobutton without looping? In MS Access
you can place radiobuttons in a groupbox, and when a radiobutton is
checked - the groupbox acquires a value like 0, 1, 2, ... so you know
which radiobutton was checked without looping. How can I achieve
similar functionality in VB.net (2005) ?

Thanks
 
Hi,

Be aware that this code gives always a change the first time

\\\
Private Sub Rad_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Rad1.CheckedChanged, _
Rad2.CheckedChanged, Rad3.CheckedChanged, Rad4.CheckedChanged,
Rad5.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
MessageBox.Show(DirectCast(sender, RadioButton).Name)
End Sub
///

I hope this helps,

Cor
 
Thanks. I was thinking about that, but I need to assign a value to a
variable based on which radiobutton was checked. This method would
require a global variable.

I was thinking something like

dim str1 As String = arrRads.IndexOftheRadThatWasChecked.Name

Another thought was to create a function --- where I would loop through
the array to find the checked radiobutton

Private Function GetRad() As String
...
Return someStringVar
End Function

Or I could create a custom radiobutton collection class.

But I was checking if there was something builtin already before I start
chiseling away at my new wheel (reinventing the wheel :).

Rich
 
Thanks. I was thinking about that, but I need to assign a value to a
variable based on which radiobutton was checked. This method would
require a global variable.
Why instead of the messagebox you can use a Select case

Cor
 
The following 5 radio buttons are contained in a groupbox on a form

Dim arrRad() As RadioButton

arrRad = New RadioButton(){rad1, rad2, rad3, rad4, rad5}

Is there a way to select which of the 5 radio buttons in the array (in
the groupbox) is the checked radiobutton without looping? In MS Access
you can place radiobuttons in a groupbox, and when a radiobutton is
checked - the groupbox acquires a value like 0, 1, 2, ... so you know
which radiobutton was checked without looping. How can I achieve
similar functionality in VB.net (2005) ?

Thanks

There is no built-in way to manage radiobuttons.

I created a class derived from GroupBox that subscribes to its
children's CheckedChanged events. I added a property that returns the
currently checked checkbox and that also programatically checks a
button.
 
Rich P said:
The following 5 radio buttons are contained in a groupbox on a form

Dim arrRad() As RadioButton

arrRad = New RadioButton(){rad1, rad2, rad3, rad4, rad5}

Is there a way to select which of the 5 radio buttons in the array (in
the groupbox) is the checked radiobutton without looping? In MS Access
you can place radiobuttons in a groupbox, and when a radiobutton is
checked - the groupbox acquires a value like 0, 1, 2, ... so you know
which radiobutton was checked without looping. How can I achieve
similar functionality in VB.net (2005) ?

Thanks

If you have placed the radiobuttons in a groupbox then only one of the
radiobuttons can be checked at one time. This means that you will receive
events for both the radiobutton that is now checked and for the radiobutton
that is no longer checked. You should be able to code to react to that
situation without much problem.

LS
 
Rich P said:
Thanks. I was thinking about that, but I need to assign a value to a
variable based on which radiobutton was checked. This method would
require a global variable.

I'd use Cor's apporach based on an event handler shared between the radio
buttons of a certain group. Just store the selected control in a private
variable and use this variable later to access the selected control.
 
Lloyd,

Is in my sample that I have send

Cor





ay to select which of the 5 radio buttons in the array (in
 
In VB6 (I do not know about .NET) this is easy:

Private Sub OptionGroupReminders_Click(Index As Integer)
intRadioButtonSelected = Index
End Sub

(The buttons need to be created as an array)
The following 5 radio buttons are contained in a groupbox on a form

Dim arrRad() As RadioButton

arrRad = New RadioButton(){rad1, rad2, rad3, rad4, rad5}

Is there a way to select which of the 5 radio buttons in the array (in
the groupbox) is the checked radiobutton without looping? In MS Access
you can place radiobuttons in a groupbox, and when a radiobutton is
checked - the groupbox acquires a value like 0, 1, 2, ... so you know
which radiobutton was checked without looping. How can I achieve
similar functionality in VB.net (2005) ?

Thanks



On Monday, March 23, 2009 2:20 PM Cor Ligthert[MVP] wrote:
Hi,

Be aware that this code gives always a change the first time

\\\
Private Sub Rad_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Rad1.CheckedChanged, _
Rad2.CheckedChanged, Rad3.CheckedChanged, Rad4.CheckedChanged,
Rad5.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
MessageBox.Show(DirectCast(sender, RadioButton).Name)
End Sub
///

I hope this helps,

Cor

news:%[email protected]...
 
Back
Top