老大要我写一个用户控件,就是一个容器,这个容器可以随便向里面添加控件//例如容器类的名称是:DuxListContainer
//如果要向容器里放控件了
DuxListContainer dux = new DuxListContainer();
//不是用:  
dux.Add("控件");
//而是要用:
dux.Items.Add("控件");//要实现上面这个就必须写一个集合类...........晕菜了,不知道怎么搞了重来没做过//就像ListView1.Items.Add();datagridview1.Rows.Add();
//都不是直接[名字].Add();而是通过Items,Rows来添加//老大还跟我说在.net2.0以前写集合类都是下面这么搞的
 public class DuxObjectCollection : ICollection, IEnumerable,IList
 {}//现在都用泛型-------------哎------------说的我云里雾里去了 这个集合类怎么写哟,并且怎么在用户控件中使用,一点思路都没得..........大侠们帮下忙呀或者谁有这个方面的资料,知道网站,哪里有下载的例子都告诉我下  帮忙了

解决方案 »

  1.   


    public class DuxListContainer : Component
    {
        private List<Control> items = new List<Control>();
        public List<Control> Items
        {
            get { return this.items; }
        }
    }
      

  2.   

    public class DuxListItem : IList<string> {}public class DuxListContainer {  public DuxListItem Items {
        get; set;
      }}
      

  3.   

    这个集合类怎么写 //我现在是用这个,用最简单的方法
    public class DuxObjectCollection : ICollection, IEnumerable,IList
     {}//实现3个接口的所有方法,然后自己在写一写方法
    大家多多帮忙呀,有这方面的东西都贴出来吧,不怕多我好参考参考.......
      

  4.   

    研究一下 Designer.cs 文件吧~~~
      

  5.   

    参考:namespace WindowsFormsApplication1
    {
        public class DuxListContainer
        {
            private DuxObjectCollection items;
            public DuxObjectCollection Items
            {
                get { return items; }
                set { items = value; }
            }        public DuxListContainer()
            {
                items = new DuxObjectCollection();
            }
        }    public class DuxObjectCollection : IList //实现IList接口就自动实现ICollection和IEnumerable接口了
        {
            private List<Control> listControl;
            public DuxObjectCollection()
            {
                listControl = new List<Control>();
            }        #region IList成员
            public int Add(object value)
            {
                if (value is Control)
                {
                    listControl.Add(value as Control);
                    return listControl.Count - 1;
                }
                else
                {
                    throw new Exception("添加的类型必须为控件");
                }
            }        public void Clear()
            {
                listControl.Clear();
            }        public bool Contains(Object value)
            {
                if (value is Control)
                    return listControl.Contains(value as Control);
                else
                    throw new Exception("类型必须为控件");
            }        public int IndexOf(Object value)
            {
                if (value is Control)
                    return listControl.IndexOf(value as Control);
                else
                    throw new Exception("类型必须为控件");
            }        public void Insert(int index, object value)
            {
                if (value is Control)
                    listControl.Insert(index, value as Control);
                else
                    throw new Exception("类型必须为控件");
            }        public void Remove(object value)
            {
                if (value is Control)
                    listControl.Remove(value as Control);
                else
                    throw new Exception("类型必须为控件");
            }        public void RemoveAt(int index)
            {
                listControl.RemoveAt(index);
            }        public void CopyTo(Array array, int index)
            {
                for (int i = 0; i < listControl.Count - 1 - index; i++)
                {
                    array.SetValue(listControl[i + index], i);
                }
            }        public IEnumerator GetEnumerator()
            {
                return listControl.GetEnumerator();
            }        public int Count
            {
                get { return listControl.Count; }
            }        public bool IsSynchronized
            {
                get { return false; }
            }        public object SyncRoot { get { return null; } }        public bool IsFixedSize { get { return false; } }        public bool IsReadOnly { get { return false; } }        public object this[int index]
            {
                get { return listControl[index]; }
                set
                {
                    if (value is Control)
                        listControl[index] = value as Control;
                    else
                        throw new Exception("类型必须为控件");
                }
            }
            #endregion
        }
    }
        public partial class Form2 : Form
        {
            DuxListContainer dux;
            private void Form2_Load(object sender, EventArgs e)
            {
                dux = new DuxListContainer();
                Button btn = new Button();
                btn.Name = "btn";
                btn.Text = "Click me";
                btn.Location = new System.Drawing.Point(0, 0);
                TextBox tb = new TextBox();
                tb.Name = "txt";
                tb.Location = new System.Drawing.Point(100, 100);
                dux.Items.Add(btn);
                dux.Items.Add(tb);
            }        private void button1_Click(object sender, EventArgs e)
            {
                foreach (Control c in dux.Items)
                {
                    this.Controls.Add(c);
                }
            }
        }
      

  6.   

    谢谢天行健  这个对我很有帮助!Google上应该用什么[关键字]去搜索,我到网上搜索的都没多大用,这方面的资料越多越好啊
      

  7.   

    你可以了解一下Panel这些容器控件的父类和接口!继承它们!
      

  8.   


            public int Add(object value)
            {
                if (value is Control)
                {
                    listControl.Add(value as Control);
                    return listControl.Count - 1;
                }
                else
                {
                    throw new Exception("添加的类型必须为控件");
                }
            }控件没有添加到我的容器里去啊 这里还只添加到listControl中,还跟我的容器扯上关系吧????
      

  9.   


            public int Add(object value)
            {
                if (value is Control)
                {
                    listControl.Add(value as Control);
                    return listControl.Count - 1;
                    //写一个事件捕获 添加
                      //容器中调用这个事件   如果集合类中添加了控件,那么相应的容器中也要添加控件                 
                      //或者在这里直接添加到容器上
                      //不知道咋搞,思路应该是这样的
                }
                else
                {
                    throw new Exception("添加的类型必须为控件");
                }
            }
      

  10.   

    参考:集合类(当然你可以自己把它写成泛型类):namespace WindowsFormsApplication1
    {
        public class DuxObjectCollection : IList
        {
            private List<Control> listControl;
            public DuxObjectCollection()
            {
                listControl = new List<Control>();
            }        public event EventHandler OnControlAdd;
            #region IList成员
            public int Add(object value)
            {
                if (value is Control)
                {
                    listControl.Add(value as Control);
                    if (OnControlAdd != null)
                    {
                        OnControlAdd(value, new EventArgs());
                    }
                    return listControl.Count - 1;
                }
                else
                {
                    throw new Exception("添加的类型必须为控件");
                }
            }        public void Clear()
            {
                listControl.Clear();
            }        public bool Contains(Object value)
            {
                if (value is Control)
                    return listControl.Contains(value as Control);
                else
                    throw new Exception("类型必须为控件");
            }        public int IndexOf(Object value)
            {
                if (value is Control)
                    return listControl.IndexOf(value as Control);
                else
                    throw new Exception("类型必须为控件");
            }        public void Insert(int index, object value)
            {
                if (value is Control)
                    listControl.Insert(index, value as Control);
                else
                    throw new Exception("类型必须为控件");
            }        public void Remove(object value)
            {
                if (value is Control)
                    listControl.Remove(value as Control);
                else
                    throw new Exception("类型必须为控件");
            }        public void RemoveAt(int index)
            {
                listControl.RemoveAt(index);
            }        public void CopyTo(Array array, int index)
            {
                for (int i = 0; i < listControl.Count - 1 - index; i++)
                {
                    array.SetValue(listControl[i + index], i);
                }
            }        public IEnumerator GetEnumerator()
            {
                return listControl.GetEnumerator();
            }        public int Count
            {
                get { return listControl.Count; }
            }        public bool IsSynchronized
            {
                get { return false; }
            }        public object SyncRoot { get { return null; } }        public bool IsFixedSize { get { return false; } }        public bool IsReadOnly { get { return false; } }        public object this[int index]
            {
                get { return listControl[index]; }
                set
                {
                    if (value is Control)
                        listControl[index] = value as Control;
                    else
                        throw new Exception("类型必须为控件");
                }
            }
            #endregion
        }
    }用户控件类:namespace WindowsFormsApplication1
    {
        public partial class DuxListContainer : UserControl
        {
            private DuxObjectCollection items;
            public DuxObjectCollection Items
            {
                get { return items; }
                set { items = value; }
            }        public DuxListContainer()
            {
                InitializeComponent(); 
                items = new DuxObjectCollection();
                items.OnControlAdd += new EventHandler(items_OnControlAdd);
            }        void items_OnControlAdd(object sender, EventArgs e)
            {
                this.Controls.Add(sender as Control);
            }
        }
    }
    把用户控件拖到窗口,窗口代码:namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            private void Form2_Load(object sender, EventArgs e)
            {
                Button btn = new Button();
                btn.Name = "btn";
                btn.Text = "Click me";
                btn.Location = new System.Drawing.Point(0, 0);
                TextBox tb = new TextBox();
                tb.Name = "txt";
                tb.Location = new System.Drawing.Point(100, 100);
                duxListContainer1.Items.Add(btn);
                duxListContainer1.Items.Add(tb);
            }
        }
    }
      

  11.   

    谢谢 ojlovecd天行健 非常感谢.在问下:  下面的其他的方法也怎样处理Remove() 这个我知道应该是跟Add()方法一样了,也要用个事件去捕获listControl中是否有控件被移除但是其他的方法了,例如Clear()等等要实现这样的操作吗?????????麻烦告诉我下 谢谢
      

  12.   

    对,差不多的,如果你觉得写这么多事件太麻烦,你可以只写一个事件来进行处理,
    比如:namespace WindowsFormsApplication1
    {
        public enum ControlChangeState
        {
            Add, Remove, Clear, RemoveAt, Insert
        }    public class DuxObjectCollection : IList
        {
            private List<Control> listControl;
            public DuxObjectCollection()
            {
                listControl = new List<Control>();
            }        public delegate void ControlChangeHandler(object sender, ControlChangeState state);
            public event ControlChangeHandler OnControlChange;
            #region IList成员
            public int Add(object value)
            {
                if (value is Control)
                {
                    listControl.Add(value as Control);
                    if (OnControlChange != null)
                    {
                        OnControlChange(value, ControlChangeState.Add);
                    }
                    return listControl.Count - 1;
                }
                else
                {
                    throw new Exception("添加的类型必须为控件");
                }
            }        public void Clear()
            {
                listControl.Clear();
                if (OnControlChange != null)
                {
                    OnControlChange(null, ControlChangeState.Clear);
                }
            }
        //以下省略了……
        }
    }
    namespace WindowsFormsApplication1
    {
        public partial class DuxListContainer : UserControl
        {
            private DuxObjectCollection items;
            public DuxObjectCollection Items
            {
                get { return items; }
                set { items = value; }
            }        public DuxListContainer()
            {
                InitializeComponent(); 
                items = new DuxObjectCollection();
                items.OnControlChange += new DuxObjectCollection.ControlChangeHandler(items_OnControlChange);
            }        void items_OnControlChange(object sender, ControlChangeState state)
            {
                switch (state)
                {
                    case ControlChangeState.Add:
                        this.Controls.Add(sender as Control);
                        break;
                    case ControlChangeState.Clear:
                        this.Controls.Clear();
                        break;
                        //以下你自己写了
                }
            }
        }
    }
      

  13.   

    ojlovecd天行健 非常的感谢你.....好了,问的差不多了,其他的自己去琢磨了..结贴了 谢谢上面的兄弟捧场