Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / January 2006

Tip: Looking for answers? Try searching our database.

How to address each of 12 radio buttons programmatically?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jan Smith - 25 Jan 2006 14:16 GMT
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.
Larry Lard - 25 Jan 2006 14:43 GMT
> 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
[quoted text clipped - 12 lines]
> 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.

Signature

Larry Lard
Replies to group please

Lebesgue - 25 Jan 2006 14:44 GMT
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.

>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
[quoted text clipped - 20 lines]
>
> Thanks for any help.
Nick Hounsome - 25 Jan 2006 15:44 GMT
>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. Yates - 25 Jan 2006 17:14 GMT
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

>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
[quoted text clipped - 20 lines]
>
> Thanks for any help.
rossum - 25 Jan 2006 22:04 GMT
>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
[quoted text clipped - 20 lines]
>
>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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.