How to address each of 12 radio buttons programmatically?

J

Jan Smith

I have a C# project that has a tab control with four tab pages. On each tab
page is a set of three radio buttons (named radioButton1 through
radioButton12). I would like to loop through the radio buttons and set
their state without having to form the complete name of each one, something
like this:

public static void SetButtonState(Form frm, int ButtonCount)

{

for (int i = 0; i < ButtonCount; i++)

{

frm.radioButton.Checked = true;

}

}

How can address each radio button without having to form its complete name,
which would require 11 extra lines of code?

Thanks for any help.
 
L

Larry Lard

Jan said:
I have a C# project that has a tab control with four tab pages. On each tab
page is a set of three radio buttons (named radioButton1 through
radioButton12). I would like to loop through the radio buttons and set
their state without having to form the complete name of each one, something
like this:

public static void SetButtonState(Form frm, int ButtonCount)
{
for (int i = 0; i < ButtonCount; i++)
{
frm.radioButton.Checked = true;
}
}

How can address each radio button without having to form its complete name,
which would require 11 extra lines of code?


In the constructor for the form (after the call to
InitializeComponent), put them all in a form-level array. OK, so you
have to have 12 lines of code to do *this*, but at least then whenever
you need them you can just loop.
 
L

Lebesgue

The easiest way is exactly how you have written it in the question - just
put them in an array and loop through the array. No rocket science.
 
N

Nick Hounsome

Jan Smith said:
I have a C# project that has a tab control with four tab pages. On each tab
page is a set of three radio buttons (named radioButton1 through
radioButton12). I would like to loop through the radio buttons

Consider creating a property extender component if you want to do this sort
of thing a lot
 
D

D. Yates

Jan,

This isn't the best way in the world to do this, but it is an option:

foreach(TabPage tp in tabControl1.Controls)
{
foreach(Control c in tp.Controls)
{
if (c is RadioButton)
{
RadioButton rb = (c as RadioButton);

if (rb.Name.EndsWith("1"))
rb.Checked = true;
}
}
}

You could check the Name or the Text property to determine which to check.

Note: The example assumes that the radio buttons are sitting on the tab
page itself. If the radio buttons are within group boxes, you will need to
interate through all the group boxes sitting on each tab page and and then
interage through group box's controls to find the radio buttons.

Hope this helps.

Dave
 
R

rossum

I have a C# project that has a tab control with four tab pages. On each tab
page is a set of three radio buttons (named radioButton1 through
radioButton12). I would like to loop through the radio buttons and set
their state without having to form the complete name of each one, something
like this:

public static void SetButtonState(Form frm, int ButtonCount)

{

for (int i = 0; i < ButtonCount; i++)

{

frm.radioButton.Checked = true;

}

}

How can address each radio button without having to form its complete name,
which would require 11 extra lines of code?

Thanks for any help.

You need to set up a container to hold all your twelve RadioButtons.
I would suggest an Array, ArrayList (.NET 1.1) or a List<RadioButton>
(.NET 2.0). Once all the buttons are in the container then you can
index them: myRadioButtonContainer

rossum
 

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