iterating through radio button collection as an array

P

Peted

I know you can iterate through a collection of radio buttons in a
panel, using a "for each in control" type iteration that c# supports,
but is it possible to iterate through the radio buttons collection, as
if they were an array of radio buttons ?

Basically i want to plonk the radio buttons on a panel then use a for
loop to index through the radio buttons with an idenex to check thier
state.

I want to do it this way becasue i want to make use of the index
integer when i find the checked radio button in the control group


thanks

Peted
 
P

PS

I know you can iterate through a collection of radio buttons in a
panel, using a "for each in control" type iteration that c# supports,
but is it possible to iterate through the radio buttons collection, as
if they were an array of radio buttons ?

You will have to cast them to RadioButton

foreach(Control c in myRadioButtonsContainer.Controls)
{

// whatever style you prefer for the casting / check

if(c is RadioButton)
{
RadioButton rb = (RadioButton)c;
}

RadioButton rb = c as RadioButton;
if(rb != null)
{

}
}

PS
 
P

Peted

Hello, thanks for th reply

i know it can be done that way, but as i said i want to know if it is
possible to iterate through those same controls without using the
"foreach" command.

i want to iterate using a for loop as if the rb controls were an array
with a and index

eg

for (int i =0; i<number of radiobuttons;i++)
radiobutton.checked

etc etc etc

thanks

Peted
 
P

PS

Hello, thanks for th reply

i know it can be done that way, but as i said i want to know if it is
possible to iterate through those same controls without using the
"foreach" command.

i want to iterate using a for loop as if the rb controls were an array
with a and index

eg

for (int i =0; i<number of radiobuttons;i++)
radiobutton.checked

etc etc etc


you can access with the indexer like
((RadionButton)myRadioButtonsContainer.Controls[3]).Checked

PS
 
B

Bruce Wood

If I were you, I would maintain my own array of radio buttons as a
separate structure, and then iterate over that.

If you want the index to have meaning, then you'll have to build the
array in code, yourself.

If it doesn't matter which index corresponds to which button, you
could build it once, in the constructor of your form, and then just
use it from then on:

ArrayList radioButtons = new ArrayList();
foreach (Control ctl in this.Controls)
{
RadioButton btn = ctl as RadioButton;
if (btn != null) radioButtons.Add(btn);
}
this._radioButtonArray =
(RadioButton[])radioButtons.ToArray(typeof(RadioButton));

Hello, thanks for th reply

i know it can be done that way, but as i said i want to know if it is
possible to iterate through those same controls without using the
"foreach" command.

i want to iterate using a for loop as if the rb controls were an array
with a and index

eg

for (int i =0; i<number of radiobuttons;i++)
radiobutton.checked

etc etc etc

thanks

Peted

You will have to cast them to RadioButton
foreach(Control c in myRadioButtonsContainer.Controls)
{
// whatever style you prefer for the casting / check
if(c is RadioButton)
{
RadioButton rb = (RadioButton)c;
}
RadioButton rb = c as RadioButton;
if(rb != null)
{
 
P

Peted

thanks for that


Hello, thanks for th reply

i know it can be done that way, but as i said i want to know if it is
possible to iterate through those same controls without using the
"foreach" command.

i want to iterate using a for loop as if the rb controls were an array
with a and index

eg

for (int i =0; i<number of radiobuttons;i++)
radiobutton.checked

etc etc etc


you can access with the indexer like
((RadionButton)myRadioButtonsContainer.Controls[3]).Checked

PS
thanks

Peted
 

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