如何在窗体中获取控件和控件的所有名称项(给窗体上的所有对象命名) [英] how do get all name items of controls and controls in form(give names everything object on form)

查看:99
本文介绍了如何在窗体中获取控件和控件的所有名称项(给窗体上的所有对象命名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取表单中控件和控件的所有名称项目

给表单 表单上的所有对象

喜欢窗口文件大纲视觉工作室但没有嵌套树



更多细节

my问题是:如何在表单上给出所有名称对象

(对象包含所有内容; ToolStrip,ToolStripButton,ToolStripMenuItem,Panel,Button,TextBox,GroupBox,MenuStrip,....)

访问链接:http://www.picpaste.com/preview-um95DYs8.gif

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

推荐答案

试试此代码..



OrignalGriff 解决方案做了一些小改动..

ToolStripItems 不是从 Control 类派生的,它们需要单独处理,如下所示。这将适用于其他非 Control Baseclass

的控件,因此必须根据Base类型进行修改。



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.

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:

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;


简单:

Simple:
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:

List<string> names = new List<string>();
GetNames(this, names);
foreach (String s in names)
    {
    Console.WriteLine(s);
    }


假设ToolStrip中唯一可能嵌套的项是DropDownButton,而DropDownButton只能有一个级别的内部控件:
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);
        }
    }
}

此示例假设表单上有两个TextBox,'textBox1和'textBox2。

This example assumes two TextBoxes are on a Form, 'textBox1, and 'textBox2.


这篇关于如何在窗体中获取控件和控件的所有名称项(给窗体上的所有对象命名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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