Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
how do get all name items of controls and controls in form
(give names everything object on form)
like window Document outline visual studio but without nested tree

More details
my question is : how to give all names objects on form
(object contains everything ; ToolStrip,ToolStripButton,ToolStripMenuItem,Panel,Button,TextBox,GroupBox,MenuStrip,.... )
visit link : http://www.picpaste.com/preview-um95DYs8.gif
Posted
Updated 26-Jan-14 18:39pm
v10
Comments
Karthik_Mahalingam 26-Jan-14 6:34am    
web or windows ?

Try this code..

Made some small modification on OrignalGriff Solution..
Since ToolStripItems are not derived from Control Class, they need to be handled separately as below. this is will be applicable for other controls which is not derived from Control Baseclass
so has to be modified as per the Base type.

C#
private void Form2_Load(object sender, EventArgs e)
       {
           List<string> names = new List<string>();
           GetNames(this, names);

           var output = names;

       }

       private void GetNames(Control c, List<string> names)
       {
           foreach (Control sub in c.Controls)
           {
               names.Add(sub.Name);
               if (sub is ToolStrip)
                   names.AddRange((sub as ToolStrip).Items.OfType<ToolStripItem>().Select(k => k.Name));
               GetNames(sub, names);
           }
       }


Alternative Method:

C#
var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
           path = path.Substring(0, path.IndexOf("bin")) + "\\" + this.Name + ".Designer.cs";

           var allines = File.ReadAllLines(path);
           int index = allines.Select(k => k.Trim()).ToList().IndexOf("#endregion");
           var names = allines.Select(k => k.Trim()).Skip(index + 1).Select(k =>
           {
               var temp = k.Split(' ');
               if (temp.Length == 3)
                   return temp[2].Replace(";", "");
               return k;
           }).Where(k => k != "" && k != "}").ToList();

           var output = names;
 
Share this answer
 
v3
Comments
ahmadamiri 26-Jan-14 6:47am    
this source no work all object (do only controls and nested controls)
but i want any object on form (example : items of toolstript and .....)
like window Document outline visualstudio but without nested tree
(give all names any thing object on form)
Karthik_Mahalingam 26-Jan-14 7:30am    
check my updated solution.
ahmadamiri 26-Jan-14 7:54am    
No work nested items of toolstrip.
EVERYTHING OBJECT
Controls,Items Control,Items of Items Controls,.........
Karthik_Mahalingam 26-Jan-14 8:03am    
i am not getting you ..
can u make it more clear.
ahmadamiri 26-Jan-14 8:16am    
check my question
its update
Simple:
C#
private void GetNames(Control c, List<string> names)
    {
    foreach (Control sub in c.Controls)
        {
        names.Add(sub.Name);
        GetNames(sub, names);
        }
    }
Then just call it from your Form:
C#
List<string> names = new List<string>();
GetNames(this, names);
foreach (String s in names)
    {
    Console.WriteLine(s);
    }
 
Share this answer
 
Comments
ahmadamiri 26-Jan-14 6:39am    
this source no work all object (do only controls)
but i want any object(example : items of toolstript and .....)
Assuming the only potentially nested item in a ToolStrip is a DropDownButton, and a DropDownButton can have only one "level" of internal controls:
List<Control> controlList = new List<Control>();

List<ToolStripItem> toolStripList = new List<ToolStripItem>();

private void Form1_Load(object sender, EventArgs e)
{
    buildControlsList(this.Controls);

    foreach (Control cnt in controlList)
    {
        textBox1.Text += cnt.Name + Environment.NewLine;
    }

    foreach (ToolStripItem tsi in toolStripList)
    {
        textBox2.Text += tsi.Name + Environment.NewLine;
    }
}

// note the requirement here to specify 'Control.ControlCollection !
private void buildControlsList(Control.ControlCollection controls)
{
    foreach (Control cnt in controls)
    {
        controlList.Add(cnt);

        // here we're willing to repeat the type conversion in the 'foreach loop
        if (cnt is ToolStrip)
        {
            foreach (ToolStripItem tsi in (cnt as ToolStrip).Items)
            {
                toolStripList.Add(tsi);

                // for variety do a trial conversion here ...
                ToolStripDropDownButton tsb = tsi as ToolStripDropDownButton;

                if (tsb != null && tsb.DropDownItems.Count > 0)
                {
                    foreach(ToolStripItem tsd in tsb.DropDownItems)
                    {
                        toolStripList.Add(tsd);
                    }
                }
            }
        }
        else
        {
            if (cnt.Controls.Count > 0) buildControlsList(cnt.Controls);
        }
    }
}
This example assumes two TextBoxes are on a Form, 'textBox1, and 'textBox2.
 
Share this answer
 

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