怎样写一个集合类,我要在自己的控件里用 就像ListView中的Items一样

wxm3630478 2009-04-22 05:08:52
老大要我写一个用户控件,就是一个容器,这个容器可以随便向里面添加控件

//例如容器类的名称是: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
{}

//现在都用泛型-------------哎------------说的我云里雾里去了



这个集合类怎么写哟,并且怎么在用户控件中使用,一点思路都没得..........大侠们帮下忙呀

或者谁有这个方面的资料,知道网站,哪里有下载的例子都告诉我下 帮忙了
...全文
237 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
readfuture 2009-04-23
  • 打赏
  • 举报
回复
貌似以前在哪见过有这样的需求;在网上找一下吧,祝你好运
我姓区不姓区 2009-04-23
  • 打赏
  • 举报
回复
参考:

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);
}
}
}

龙宜坡 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gomoku 的回复:]
C# codepublicclassDuxListContainer : Component
{privateList<Control>items=newList<Control>();publicList<Control>Items
{get{returnthis.items; }
}
}
[/Quote]


泛型List就成!
wxm3630478 2009-04-23
  • 打赏
  • 举报
回复
其他的像我这样是新手的朋友这里把这个东东收藏了,很有用的了
wxm3630478 2009-04-23
  • 打赏
  • 举报
回复
ojlovecd天行健 非常的感谢你.....好了,问的差不多了,其他的自己去琢磨了..

结贴了 谢谢上面的兄弟捧场
我姓区不姓区 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 wxm3630478 的回复:]
谢谢 ojlovecd天行健 非常感谢.

在问下:  下面的其他的方法也怎样处理

Remove() 这个我知道应该是跟Add()方法一样了,也要用个事件去捕获listControl中是否有控件被移除

但是其他的方法了,例如Clear()等等要实现这样的操作吗?????????

麻烦告诉我下 谢谢

[/Quote]
对,差不多的,如果你觉得写这么多事件太麻烦,你可以只写一个事件来进行处理,
比如:

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;
//以下你自己写了
}
}


}
}

wxm3630478 2009-04-23
  • 打赏
  • 举报
回复
谢谢 ojlovecd天行健 非常感谢.

在问下: 下面的其他的方法也怎样处理

Remove() 这个我知道应该是跟Add()方法一样了,也要用个事件去捕获listControl中是否有控件被移除

但是其他的方法了,例如Clear()等等要实现这样的操作吗?????????

麻烦告诉我下 谢谢
我姓区不姓区 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wxm3630478 的回复:]
C# codepublicintAdd(objectvalue)
{if(valueisControl)
{
listControl.Add(valueasControl);returnlistControl.Count-1;//写一个事件捕获 添加//容器中调用这个事件 如果集合类中添加了控件,那么相应的容器中也要添加控件//或者在这里直接添加到容器上//不知道咋搞,思路应该是这样的}else{thrownewException("添加的类型必须为控件");
}
}
[/Quote]
参考:

集合类(当然你可以自己把它写成泛型类):

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);
}
}
}

wxm3630478 2009-04-23
  • 打赏
  • 举报
回复

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

wujinjian2008n 2009-04-23
  • 打赏
  • 举报
回复
up
wxm3630478 2009-04-23
  • 打赏
  • 举报
回复

public int Add(object value)
{
if (value is Control)
{
listControl.Add(value as Control);
return listControl.Count - 1;
}
else
{
throw new Exception("添加的类型必须为控件");
}
}

控件没有添加到我的容器里去啊 这里还只添加到listControl中,还跟我的容器扯上关系吧????
jietuan 2009-04-23
  • 打赏
  • 举报
回复
你可以了解一下Panel这些容器控件的父类和接口!继承它们!
我姓区不姓区 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wxm3630478 的回复:]
我还没完成了,大家继续帮忙啊,进来顶下也行哈
[/Quote]
还有什么需求?
wxm3630478 2009-04-23
  • 打赏
  • 举报
回复
我还没完成了,大家继续帮忙啊,进来顶下也行哈
wxm3630478 2009-04-23
  • 打赏
  • 举报
回复
谢谢天行健 这个对我很有帮助!

Google上应该用什么[关键字]去搜索,我到网上搜索的都没多大用,这方面的资料越多越好啊
xufzu123 2009-04-22
  • 打赏
  • 举报
回复
没做过~学习~~
uncleson88 2009-04-22
  • 打赏
  • 举报
回复
研究一下 Designer.cs 文件吧~~~
wxm3630478 2009-04-22
  • 打赏
  • 举报
回复
这个集合类怎么写

 //我现在是用这个,用最简单的方法
public class DuxObjectCollection : ICollection, IEnumerable,IList
{}

//实现3个接口的所有方法,然后自己在写一写方法


大家多多帮忙呀,有这方面的东西都贴出来吧,不怕多我好参考参考.......
wfyfngu 2009-04-22
  • 打赏
  • 举报
回复
public class DuxListItem : IList<string> {}

public class DuxListContainer {

public DuxListItem Items {
get; set;
}

}
gomoku 2009-04-22
  • 打赏
  • 举报
回复

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

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧