471,605 Members | 1,344 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

How to address each of 12 radio buttons programmatically?

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[i].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.


Jan 25 '06 #1
5 3440

Jan Smith wrote:
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[i].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.

--
Larry Lard
Replies to group please

Jan 25 '06 #2
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.

"Jan Smith" <JS@nospam.com> wrote in message
news:Lq******************************@giganews.com ...
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[i].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.

Jan 25 '06 #3

"Jan Smith" <JS@nospam.com> wrote in message
news:Lq******************************@giganews.com ...
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
Jan 25 '06 #4
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

"Jan Smith" <JS@nospam.com> wrote in message
news:Lq******************************@giganews.com ...
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[i].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.

Jan 25 '06 #5
On Wed, 25 Jan 2006 09:16:30 -0500, "Jan Smith" <JS@nospam.com> wrote:
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[i].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[i]

rossum


--

The ultimate truth is that there is no ultimate truth
Jan 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John Davis | last post: by
reply views Thread by vinay | last post: by
2 posts views Thread by tshad | last post: by
reply views Thread by tshad | last post: by
7 posts views Thread by IchBin | last post: by
2 posts views Thread by dpazza | last post: by
10 posts views Thread by =?Utf-8?B?UGFycm90?= | last post: by
3 posts views Thread by tshad | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.