Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i have one project(C#2.0) and i used multiple form in that project.i want all form name and all controls name in each form .pls send the answer to my email id [mr.s.murugesan@gmail.com]

Thanks & Regards
S.Murugesan
Posted
Updated 4-Feb-10 3:50am
v3

You can iterate through My.Application.OpenForms to list all opened forms. Alternatively, you could follow this[^] kb-article.

You can iterate through the controls on a specific form by looping through the Controls[^]-collection of the form.

Good luck :)
 
Share this answer
 
If you want just to calculate you might go for this :

Assembly asm = Assembly.GetExecutingAssembly();
Type[] tobjects = asm.GetTypes();
List<string> forms = new List<string>();
foreach (Type tobject in tobjects)
{
    if(tobject.IsSubclassOf(typeof(Form)))
    {
        forms.Add(tobject);
        Form frm = Activator.CreateInstance(tobject) as Form;
        foreach(Controls in frm.Controls)
         //calculate here.
    }
}


The list forms will hold all the types that are within the executing assembly.

Now you can easily create instance of each types and calculate the Controls within them.

I hope this will help you.
Cheers.
:rose:
 
Share this answer
 
This is the example code to get the hierarchical data with all Form names and embedded control names in "Text" property of TreeViewItem. You just need to call call "GetAllControl" function to iterate on forms.


C#
void AddControl(TreeNodeCollection collection, Control ctrl)
{
    TreeNode node = new TreeNode(ctrl.Name);
    foreach (Control c in ctrl.Controls)
        AddControl(node.Nodes, c);
    collection.Add(node);
}
private TreeNodeCollection GetAllControls()
{ 
    TreeNode rootNode = new TreeNode();
    foreach (Form form in Application.OpenForms)
        AddControl(rootNode.Nodes, form);
    return rootNode.Nodes;
}


Usage Example:

C#
private void Form1_Load(object sender, EventArgs e)
{
    foreach (TreeNode node in GetAllControls())
        treeView1.Nodes.Add(node);
}


P.S. I saw that I copied wring code in my previous answer, I tried to improve it but it is not updating. I don't know why is that, so I am posting another answer. Sorry for mess.
 
Share this answer
 
This is the example code to get the hierarchical data with all Form names and embedded control names in "Text" property of TreeViewItem. You just need to call call "GetAllControls" function to iterate on forms.


C#
void AddControl(TreeNodeCollection collection, Control ctrl)
{
    TreeNode node = new TreeNode(ctrl.Name);
    foreach (Control c in ctrl.Controls)
        AddControl(node.Nodes, c);
    collection.Add(node);
}
private void Form1_Load(object sender, EventArgs e)
{
    foreach (TreeNode node in GetAllControls())
        treeView1.Nodes.Add(node);
}


Usage Example:

C#
private void Form1_Load(object sender, EventArgs e)
{
    foreach (TreeNode node in GetAllControls())
        treeView1.Nodes.Add(node);
}
 
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