473,554 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to address each of 12 radio buttons programmaticall y?

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 3547

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
InitializeCompo nent), 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.co m> wrote in message
news:Lq******** *************** *******@giganew s.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.co m> wrote in message
news:Lq******** *************** *******@giganew s.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.Con trols)
{
foreach(Control c in tp.Controls)
{
if (c is RadioButton)
{
RadioButton rb = (c as RadioButton);

if (rb.Name.EndsWi th("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.co m> wrote in message
news:Lq******** *************** *******@giganew s.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.co m> 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<RadioButto n>
(.NET 2.0). Once all the buttons are in the container then you can
index them: myRadioButtonCo ntainer[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
3110
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, the text will change to lowercase. I added the following event, but still won't able to change the text to uppercase. Private Sub...
0
1799
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 conversion). When the user enters a text in >text field, the user can click the radio button for case conversion. I set >the property of radio button to...
2
1580
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" runat="server" Text="No"/> This is set as a group so that you can only choose one button. If you choose the other button, the first button will...
0
1113
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. I have 3 buttons: <asp:RadioButton id="SalaryPerYear" Checked="true" GroupName="SalaryPer" runat="server" text="Year" />&nbsp; <asp:RadioButton...
1
2550
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 text box and two radio buttons. The text box has the TabStop = True, and both radio buttons have TabStop = False. The radio buttons are set to...
7
3303
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 of the hyphens, quotes) my fault! 2 - If I delete the <Formand </Formstatements I can build the table of radio buttons correctly, HTML wise. The...
2
5881
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 the form by changing the visible state of the radio buttons and labels utilising back and next buttons. E.g. Next button makes current radio...
10
2188
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 checked instead of the 5th or 6th etc. I followed the logic thru the debugger and can see where the 5th button is progammatically checked but the...
3
2011
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 each). When I first start the program none of the radiobuttons are checked. I also have 4 buttons on the bottom of the page. For some reason,...
0
7603
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7806
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8047
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2021
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1139
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
845
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.