110,529
社区成员
发帖
与我相关
我的任务
分享
//例如容器类的名称是: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
{}
//现在都用泛型-------------哎------------说的我云里雾里去了
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);
}
}
}
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;
//以下你自己写了
}
}
}
}
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);
}
}
}
public int Add(object value)
{
if (value is Control)
{
listControl.Add(value as Control);
return listControl.Count - 1;
//写一个事件捕获 添加
//容器中调用这个事件 如果集合类中添加了控件,那么相应的容器中也要添加控件
//或者在这里直接添加到容器上
//不知道咋搞,思路应该是这样的
}
else
{
throw new Exception("添加的类型必须为控件");
}
}
public int Add(object value)
{
if (value is Control)
{
listControl.Add(value as Control);
return listControl.Count - 1;
}
else
{
throw new Exception("添加的类型必须为控件");
}
}
控件没有添加到我的容器里去啊 这里还只添加到listControl中,还跟我的容器扯上关系吧????
//我现在是用这个,用最简单的方法
public class DuxObjectCollection : ICollection, IEnumerable,IList
{}
//实现3个接口的所有方法,然后自己在写一写方法
public class DuxListItem : IList<string> {}
public class DuxListContainer {
public DuxListItem Items {
get; set;
}
}
public class DuxListContainer : Component
{
private List<Control> items = new List<Control>();
public List<Control> Items
{
get { return this.items; }
}
}