我需要实现的功能是,在设计时让控件(假设为ButtonEx)选择同一个窗体(假设为Form1)上的其他控件(还有Button1,Button2 等 )
这个控件的一个属性如下
        [EditorAttribute(typeof(BindControlsEditor), typeof(System.Drawing.Design.UITypeEditor))]  
        public Hashtable BindControls
        {
            get { return _BindControls; }
            set { _BindControls = value; }
        }
在BindControlsEditor 的编辑窗体中的代码//topControl 既为 Form1
                Type type = topControl.GetType();                BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Public;
                FieldInfo[] fis = type.GetFields(flag);
                List<FieldInfo> list = new List<FieldInfo>();
                if (fis != null && fis.Length > 0)
                {
                    for (int i = 0; i < fis.Length; i++)
                    {
                        object value = fis[i].GetValue(topControl);                        if (value != null)
                        {
                            if (!(value is ICommandControl) && value is Control)
                            {
                                list.Add(fis[i]);
                            }
                        }
                        else
                        {
                        }
                    }
                }
                this.dataGridView1.DataSource = list;在设计时取不到Form1里的控件
而在运行时可以正常获取到,请问有什么办法可以设计时获取到呢?

解决方案 »

  1.   

    在ButtonEx初始化的时候选择控件,也就是在构造函数中
      

  2.   

    为什么要用反射,做一个基类form,继承就行了啊
      

  3.   


    我的目的是让ButtonEx 在设计时选择与某些控件相关
      

  4.   

    我的目的是让ButtonEx 在设计时选择与某些控件相关
      

  5.   

    你是遍历控件吗?
            /// <summary>
            /// 根据控件类型查找控件
            /// </summary>
            /// <param name="parent"></param>
            /// <param name="type"></param>
            /// <returns></returns>
            public static Control[] FindControls(Control parent, Type type)
            {
                ArrayList list = new ArrayList();
                _FindControls(list, parent, type);
                if (list.Count > 0)
                {
                    Control[] ctrls = new Control[list.Count];
                    for (int i = 0; i < list.Count; i++)
                    {
                        ctrls[i] = list[i] as Control;
                    }
                    return ctrls;
                }
                else
                {
                    return null;
                }
            }
            /// <summary>
            /// 递归调用将找到的控件加入arrayList
            /// </summary>
            /// <param name="arrayList"></param>
            /// <param name="parent"></param>
            /// <param name="type"></param>
            private static void _FindControls(ArrayList arrayList, Control parent, Type type)
            {
                foreach (Control ctrl in parent.Controls)
                {
                    if (ctrl.GetType().Equals(type))
                    {
                        arrayList.Add(ctrl);
                    }                if (ctrl.HasControls())
                    {
                        _FindControls(arrayList, ctrl, type);
                    }
                }
            }