Solved

How can i get the collections of controls on my current form in C#.net window application??

Posted on 2011-02-28
16
274 Views
Last Modified: 2012-05-11
i want to know that How can i get the collections of controls on my current form ??

for ex.

suppose i have 12 radiobuttons on my current form then how can i get the collections for that using controlcollections command ??

i using the below code ...
ControlCollection collection = new ControlCollection(this.FindForm());

what can i do now ??
0
Comment
Question by:Parth48
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34996489
You can easily use LINQ to perform this
this.Controls.OfType<RadioButton>

See http://www.linqhelp.com/linq-101/introduction-to-linq-interacting-with-controls-using-linq-in-c/ for more information and examples
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 34996536
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34999178
The non-Linq approach:
ControlCollection collection = new ControlCollection(this);

foreach (Control c in this.Controls)
{
    if (c is RadioButton)
    {
        collection.Add(c);
    }
}

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:Parth48
ID: 35004134
hi @kaufmed: u r right ...

but how can i find how many radio buttons checked true ??

is there any if condition in your coding ??
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 35004173
Hi
You can check whether radio button is checked as below.
if (c is RadioButton)
 {
 RadioButton tempC = c as RadioButton;
// Check whether radio button is checked.
if (tempC.Checked)
{
 collection.Add(c);
 }
 }
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35004604
You can find all the checked radiobuttons in one query


foreach (RadioButton radio in this.Controls.OfType<RadioButton>().Where( cb => cb.Checked == true))
            {
                ...
            }
0
 

Author Comment

by:Parth48
ID: 35005694
i can't get the radiobutton , what can i do ??

one thing is that i created radiobutton dynamically , so how can i find them ??

please refer the below code ...

//creating radio buttons

                    //Radio button 1
                    RadioButton rdbOption11a = new RadioButton();
                    rdbOption11a.Location = new Point(30, 65);
                    rdbOption11a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    //rdbOption11a.Text = char.ConvertFromUtf32(97 + i).ToString();
                    rdbOption11a.Text = "a";
                    rdbOption11a.Size = new Size(40, 24);

                    //Radio button 2
                    RadioButton rdbOption12a = new RadioButton();
                    rdbOption12a.Location = new Point(30, 95);
                    rdbOption12a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    //rdbOption11a.Text = char.ConvertFromUtf32(97 + i).ToString();
                    rdbOption12a.Text = "b";
                    rdbOption12a.Size = new Size(40, 24);

                    //Radio button 3
                    RadioButton rdbOption13a = new RadioButton();
                    rdbOption13a.Location = new Point(30, 125);
                    rdbOption13a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    //rdbOption11a.Text = char.ConvertFromUtf32(97 + i).ToString();
                    rdbOption13a.Text = "c";
                    rdbOption13a.Size = new Size(40, 24);

                    //Radio button 4
                    RadioButton rdbOption14a = new RadioButton();
                    rdbOption14a.Location = new Point(30, 155);
                    rdbOption14a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    //rdbOption11a.Text = char.ConvertFromUtf32(97 + i).ToString();
                    rdbOption14a.Text = "d";
                    rdbOption14a.Size = new Size(40, 24);

Open in new window


how can i find them ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35005769
Are the radiobuttons directly on your form or are they on a grouping-box (like a tabpage, ....)

If so, you need to use some kind of recursion

In code below, I just created a very small winapp, containing a tabpage with 2 radiobuttons (of which one was selected) and a radiobutton directly on the form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Forms_C
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
// this will give a radioButtonList.Count() of 1
            var radioButtonList = this.Controls.OfType<RadioButton>().Where(cb => cb.Checked == true);
// this will give a radioButtonList.Count() of 2

            var radioButtonList2 = Generic.FlattenChildren(this).OfType<RadioButton>().Where(cb => cb.Checked == true);

        }


    }

    public static class Generic
    {
        public static IEnumerable<Control> FlattenChildren(this Control control)
        {
            var children = control.Controls.Cast<Control>();
            return children.SelectMany(c => FlattenChildren(c)).Concat(children);
        } 
    }

Open in new window

0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 35005773
I think u missed adding the radio button to form control list add them
this.Controls.Add(rdbOption11a);
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 35005786
Like wise add this all radio buttons. As ur creating dynamically u need to add these controls to Control collection, then only u can retrieve it from it.
this.Controls.Add(rdbOption12a);
this.Controls.Add(rdbOption13a);
this.Controls.Add(rdbOption14a);
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35005793
@Sudhakar-Pulivarthi - he's doing that (I saw that in another question where he asked for mimizing the code with a loop-statement)
0
 

Author Comment

by:Parth48
ID: 35005941
i add them using the below line ..

panel.Controls.Add(lblnumber);
panel.Controls.Add(lblRandomQuestion11);
tabPage.Controls.Add(panel);

Open in new window


what can i do now ??
how can i find them ?
0
 

Author Comment

by:Parth48
ID: 35005943
one thing is that i add all the controls to the tab control ....
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35005978
See my posted code-example. Create a new class with the flattenControls-function and call that one

public static class Generic
    {
        public static IEnumerable<Control> FlattenChildren(this Control control)
        {
            var children = control.Controls.Cast<Control>();
            return children.SelectMany(c => FlattenChildren(c)).Concat(children);
        }
    }
0
 

Author Comment

by:Parth48
ID: 35006101
hi @Dhaest: yes u r right but how can i convert this result into string array ...

var radioButtonList2 = Generic.FlattenChildren(this).OfType<RadioButton>().Where(cb => cb.Checked == true);

Open in new window


above code give checked textbox but how can i convert this result into string array ??

for ex.

string[] correopt = new string[3];

correopt = ???

how can i convert ?
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 35006185
Actually the radioButtonList2 is an array, why with a reference to the control.
I would use this list for later loops...

Foreach(RadioButton rbn in radioButtonList2)
{
...    rbn.Name  ...
}
Do you just want to store the names of the controls ?

           
string[] correopt = new string[radioButtonList2.Count()];
            for (int i = 0; i < radioButtonList2.Count();i++ )
            {
                correopt[i] = radioButtonList2.ElementAt(i).Name;
            }

Open in new window

0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question