473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 3531

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Davis | last post by:
I created a ASP.NET Web Form using VB.NET with a text box, 2 radio buttons. When the user click the first radio button, the text will change to uppercase. If the user clicks the other radio button,...
0
by: vinay | last post by:
why are u not using the radiobutton list??? vinay >-----Original Message----- >I created a simple ASP.NET application with a text field, and 2 radio >buttons (uppercase and lowercase...
2
by: tshad | last post by:
I have 2 radio buttons set to yes or no: <asp:RadioButton id="RelocateYes" Checked="true" GroupName="Relocate" runat="server" Text="Yes"/> <asp:RadioButton id="RelocateNo" GroupName="Relocate"...
0
by: tshad | last post by:
I am trying to set my radio buttons Programmatically. But no matter what I do - the 3rd one is always checked - even though I can tell my my trace statements the other buttons were the ones set. ...
1
by: Shane | last post by:
I think that I found a bug in the Radio Button, and I wanted to give everybody a "Heads Up". I spent three days doing trial and error until I found a work around. I created a user control with a...
7
by: IchBin | last post by:
I am trying to programmatically set a radio button in a table of radio buttons. I have two problems with the code below: 1 - I can not prepare the <Formstatement to be printed by php. (syntax...
2
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on...
10
by: =?Utf-8?B?UGFycm90?= | last post by:
I have 8 radio buttons on my Windows form but I can only select up to the first 4. If I click on any button beyond the 4th one and then come back into the program again the 4th button is always...
3
by: tshad | last post by:
I have a form that has 2 sets of radiobuttons. As it happens I created 1 radiobutton and then copied it a pasted it 4 times. I have 4 radiobuttons split up into 2 groupboxes (2 radiobuttons...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.