获取所有表格和所有控件名称 [英] Get all form and all control name

查看:95
本文介绍了获取所有表格和所有控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目(C#2.0),并且在该项目中使用了多种表单.我希望每种表单中的所有表单名称和所有控件名称.pls将答案发送至我的电子邮件ID [mr.s.murugesan@gmail .com]

谢谢与问候
S.Murugesan

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

推荐答案

您可以遍历My.Application.OpenForms列出所有打开的表单.或者,您可以按照 [ ^ ] kb-article.

您可以通过循环访问控件 [^ ]-表单的集合.

祝您好运:)
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 :)


如果您只是想计算一下,可以考虑以下方法:

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.
    }
}



列表forms 将保存正在执行的程序集中的所有类型.

现在,您可以轻松创建每种类型的实例并计算其中的控件.

我希望这能帮到您.
干杯.
:rose:



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:


这是示例代码,用于在TreeViewItem的文本"属性中获取具有所有表单名称和嵌入式控件名称的层次结构数据.您只需要调用"GetAllControl"函数即可在表单上进行迭代.


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.


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:

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



P.S.我看到我在先前的答案中复制了拧干的代码,我试图对其进行改进,但它没有更新.我不知道为什么会这样,所以我要发布另一个答案.抱歉搞砸了.



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.


这篇关于获取所有表格和所有控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆