设计时获取窗体中所有组件名称
2013-03-24 11:00
141 查看
If you are hosting windows forms designer and using DesignerSurface, all components on the form that are visible in design time are accessible through DesignerSurface.ComponentsContainer property:
Regards,
vladimir
for (int i = 0; i < surface.ComponentContainer.Components.Count; i++) { Console.WriteLine((i + 1) + ". " + surface.ComponentContainer.Components[i].Site.Name + ": " + surface.ComponentContainer.Components[i].GetType().FullName); }
host.CreateComponent(typeof(Form)); Form frm1 = (Form)Activator.CreateInstance(frmType); Form frm2 = (Form)host.RootComponent; //FieldInfo fi = frmType.GetField("components", BindingFlags.Instance | BindingFlags.NonPublic); //Container cn = null; //if (fi != null) // cn = (Container)fi.GetValue(frm1); //if (cn != null) // foreach (Component cp in cn.Components) // { // host.Container.Add(cp); // ComponentDesigner componentDesigner1 = ((ComponentDesigner)host.GetDesigner(((IComponent)cp))); // if (componentDesigner1 != null) // componentDesigner1.InitializeExistingComponent(((IDictionary)null)); // } FieldInfo[] fis = frmType.GetFields( BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo f in fis) { object o = f.GetValue(frm1); if (o is Control) continue; if (o is Component) { Component cp = (Component)o; host.Container.Add(cp); ComponentDesigner componentDesigner1 = ((ComponentDesigner)host.GetDesigner(((IComponent)cp))); if (componentDesigner1 != null) componentDesigner1.InitializeExistingComponent(((IDictionary)null)); } }
private void button1_Click(object sender, EventArgs e) { Type t = typeof(Form1); object frm = Activator.CreateInstance(t); FieldInfo field = t.GetField("components", BindingFlags.Instance | BindingFlags.NonPublic); if (field != null) { IContainer componentsContainer = field.GetValue(frm) as IContainer; if (componentsContainer != null) { // access components ny name, by index, in a loop ... for (int i = 0; i < componentsContainer.Components.Count; i++) { Console.WriteLine(i + ". component: " + componentsContainer.Components[i].GetType().FullName); } } } }
Regards,
vladimir
相关文章推荐
- Servlet获取HTML表单上所有组件名称
- WinForm开发中,将Excel文件导入到DataGridView中时,获取Excel中所有表格的名称。
- C#获取IIS所有站点及虚拟目录和应用程序(包含名称及详细信息)
- 获取Java对象中所有的属性名称和属性值
- Atitit. 数据库-----catalog与schema的设计区别以及在实际中使用 获取数据库所有库表 java jdbc php c#.Net
- [置顶] 自己写代码生成器之生成Dal层代码(获取数据库所有表名称)
- 获取ACCESS2000数据库中的所有表的名称(vc + ado)
- vb获取数据库中所有表的名称
- php获取数据库所有表名称
- 解析大型.NET ERP系统核心组件 查询设计器 报表设计器 窗体设计器 工作流设计器 任务计划设计器
- 一次性获取页面name属性相同的所有组件值
- android 里面 如何使用gson 快速获取类的所有字段名称,包括父类的
- C# 获取传入对象的所有属性名称
- c#获取数据库中所有表名称的方法
- 获取所有wlan的名称和信号量
- shell获取目录下所有文件夹的名称并输出的实例
- 获取一个数据库中的所有表的名称、一个表中所有字段的名称
- SQL Server 中获取数据库所有表及其字段名称、类型、长度的SQL
- MyGeneration获取所有ORACLE数据库的数据库名数据表命列名称
- Shell获取某目录下所有文件夹的名称