老大要我写一个用户控件,就是一个容器,这个容器可以随便向里面添加控件//例如容器类的名称是: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
{}//现在都用泛型-------------哎------------说的我云里雾里去了 这个集合类怎么写哟,并且怎么在用户控件中使用,一点思路都没得..........大侠们帮下忙呀或者谁有这个方面的资料,知道网站,哪里有下载的例子都告诉我下 帮忙了
//如果要向容器里放控件了
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
{}//现在都用泛型-------------哎------------说的我云里雾里去了 这个集合类怎么写哟,并且怎么在用户控件中使用,一点思路都没得..........大侠们帮下忙呀或者谁有这个方面的资料,知道网站,哪里有下载的例子都告诉我下 帮忙了
解决方案 »
- treeview展开后,怎么隐藏节点的图片?
- botton事件为什么没触发弹出框
- 继承ToolStripControlHost 生成控件问题。
- 我的VS2008的新建项目里没有“安装和部署项目”?
- vb中webbrowser控件属性中的object是什么意思【转成C#】
- 参照rfc 邮件协议 编写的 发邮件的程序为什么发出去是在垃圾筒里?
- 寻找一个文本框控件、
- C#利用反射实例化List<className>(未知类名的情况下)
- 请教一个问题,急!!读取.txt文件数据到datatable的问题!谢谢大家
- 怎样用C#.net实现用javascript建立xmldom对象,并从xml中读数据
- 为什么通过process启动的软件有些功能无法使用,与正常启动有何区别?
- c#连接oracle除了 OracleClient.dll 还需要什么
public class DuxListContainer : Component
{
private List<Control> items = new List<Control>();
public List<Control> Items
{
get { return this.items; }
}
}
get; set;
}}
public class DuxObjectCollection : ICollection, IEnumerable,IList
{}//实现3个接口的所有方法,然后自己在写一写方法
大家多多帮忙呀,有这方面的东西都贴出来吧,不怕多我好参考参考.......
{
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);
}
}
}
public int Add(object value)
{
if (value is Control)
{
listControl.Add(value as Control);
return listControl.Count - 1;
}
else
{
throw new Exception("添加的类型必须为控件");
}
}控件没有添加到我的容器里去啊 这里还只添加到listControl中,还跟我的容器扯上关系吧????
public int Add(object value)
{
if (value is Control)
{
listControl.Add(value as Control);
return listControl.Count - 1;
//写一个事件捕获 添加
//容器中调用这个事件 如果集合类中添加了控件,那么相应的容器中也要添加控件
//或者在这里直接添加到容器上
//不知道咋搞,思路应该是这样的
}
else
{
throw new Exception("添加的类型必须为控件");
}
}
{
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);
}
}
}
比如: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;
//以下你自己写了
}
}
}
}