Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
design a windows form with different controls. Click the button the message shows how many controls are used in that form, and his name and type to show... How...?
Posted
Comments
Maciej Los 15-Dec-14 13:06pm    
What you mean by "message shows how many controls are used in that form"?

In January, 2014, I posted an answer to a CP QA question that shows how to get all the Controls using a 'Stack, rather than using the usual recursive technique: [^].

That answer has complete source code, and examples, and links to where I got the idea from, and links to discussion of using the Stack based technique by people much smarter than I will ever be.

Modifying this code to build a record of each Control Type found, and a List of instances of each Type of Control can be done like this:
C#
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

public static class SpecialMethods
{
    // added 12/15/2014 by bw
    public static Dictionary<string,List<Control>> GetControlsByType(Control aControl)
    {
        // see comments
        return GetAllControls(aControl)
            .GroupBy(typ => typ.GetType().Name)
            .Select(grp => grp.ToList())
            .ToDictionary(name => name[0].GetType().Name);
    }

    public static IEnumerable<Control> GetAllControls(Control aControl)
    {
        Stack<Control> stack = new Stack<Control>();
 
        stack.Push(aControl);
 
        while (stack.Any())
        {
            var nextControl = stack.Pop();
 
            foreach (Control childControl in nextControl.Controls) stack.Push(childControl);

            yield return nextControl;
        }
    }
}
Now we can test this using a Form (or any other Control that may have Child Controls):
C#
private void Form1_Load(object sender, EventArgs e)
{
    foreach (KeyValuePair<string,List<Control>> kvp in SpecialMethods.GetControlsByType(this))
    {
        Console.WriteLine("Control Type: {0} Count: {1}", kvp.Key, kvp.Value.Count.ToString());
    }
}
Here's a sample output from a test:
Control Type: Form1 Count: 1
Control Type: ToolStrip Count: 2
Control Type: Button Count: 4
Control Type: TextBox Count: 1
Control Type: Label Count: 3
Control Type: GroupBox Count: 1
Control Type: Panel Count: 2
Comments:

What's happening in the 'GetControlsByType static method:

1. all the Controls are filtered into a set of IGroupings based on the name of the Type of each Control: by the GroupBy operation

2. each IGrouping is transformed into a List<Control> by the 'Select operation.

3. a Dictionary is created with the key set to the Name of the Type of the first Control in each List<Control> in each Group.
 
Share this answer
 
v3
Comments
Marcin Kozub 15-Dec-14 13:39pm    
Nice, my 5!
Member 10195652 16-Dec-14 11:12am    
thank u...frnd...
Marcin Kozub 16-Dec-14 11:37am    
You should post that comment directly to Bill Woodruff's answer :)
Maciej Los 15-Dec-14 15:15pm    
Even if i don't know what OP wants to achieve, this answer is very interesting ;)
BillWoodruff 15-Dec-14 21:08pm    
Thanks, Maciej.
Iterate through the Form.Controls collection - it contains all the controls the form has. You will probably need to be recursive: some controls (such as Panel) can contain their own Controls collection as well.
 
Share this answer
 
Comments
Member 10195652 15-Dec-14 11:29am    
how to use it...?
Need some example please...!
OriginalGriff 15-Dec-14 11:40am    
You don't know how to iterate a collection?
foreach (Control c in Controls)
Afzaal Ahmad Zeeshan 15-Dec-14 11:55am    
I am pretty much sure, if he knew iteration, he would have better known this whole scenario.

Don't you think?
Maciej Los 15-Dec-14 15:16pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900