zoukankan      html  css  js  c++  java
  • 找到当前项目中的所有窗体的控件

     List<Form> names = new List<Form>();
                List<string> GridList = new List<string>();
                foreach (var type in this.GetType().Assembly.GetTypes())
                {
                    if (typeof(Form).IsAssignableFrom(type))
                    {
                        try
                        {
                            using (Form form = Activator.CreateInstance(type) as Form)
                            {
                                names.Add(form); //names.Add(form.name)
                            }
                        }
                        catch
                        {
                            Console.WriteLine(string.Format("类型为 {0} 的窗体没有无参的构造函数,无法获取其名称", type));
                        }
                    }
                }
    
                foreach (Form item in names)
                { 
                    foreach (var field in item.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
                    {
                        if (field.FieldType == typeof(GridControl))
                        {
                                GridList.Add(field.Name); //names.Add(form.name)
                        }
                    }
                }
                string s = "";
                foreach (var item in GridList)
                {
                    s += item+",";
                   
                }
                MessageBox.Show(s);

     第二种方法

                string path = Application.StartupPath + "\layout";
                List<Form> names = new List<Form>();
                List<string> GridList = new List<string>();
                foreach (var type in Assembly.GetExecutingAssembly().GetExportedTypes())
                {
                    if (typeof(Form).IsAssignableFrom(type))
                    {
                        try
                        {
                            using (Form form = Activator.CreateInstance(type) as Form)
                            {
                                names.Add(form); //names.Add(form.name)
                            }
                        }
                        catch
                        {
                            Console.WriteLine(string.Format("类型为 {0} 的窗体没有无参的构造函数,无法获取其名称", type));
                        }
                    }
                }
                foreach (Form item in names)
                {
                    foreach (var field in item.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
                    {
                        if (field.FieldType == typeof(GridControl))
                        {
                            //GridList.Add(field.Name); //names.Add(form.name)
                            GridControl Grid = new GridControl();
                            Grid.Name = field.Name;
                            try
                            {
                                Grid.MainView.RestoreLayoutFromXml(path + "\" + item.Name + "窗体的" + field.Name);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
  • 相关阅读:
    String.valueOf()方法的使用
    springMVC中ModelAndView学写笔记
    赛邮云通信
    DOS命令
    完全二叉树一维数组存放的结点相关关系
    Float与二进制之间的转化(Java实现)
    碎片知识1
    hashtable——散列表
    Huffman Tree
    Unable to update the EntitySet 'T_JsAPI' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
  • 原文地址:https://www.cnblogs.com/iowoi/p/12529632.html
Copyright © 2011-2022 走看看