添加控件数组
在 .NET 里面我好像没有找到有关于控件数组的说明 , 但是前两天偶在网上看到了一篇关于如何在 .NET 里面实现控件数组的文章 ( 该文章请参看 MSDN). 记得大学的时候在使用 VB 的时候使用过控件数组 , 可是到了 .NET 的时代好像没有了 . 当时可以用控件数组作很多繁琐的事情 , 可以动态的生成一些功能和目的基本相同的一组文本框和一堆标签 . 这些控件数组响应同一个事件 , 我们在使用它的时候可以直接通过索引来访问 . 我使用 .NET 以后好像绑定让一切变的如此简单 , 好像控件数组没有什么用了 , 但是在前面的一次开发中我还是偶然的想到了使用控件数组 , 苦于不知道如何申明后来发现 .NET 不支持这个东东了 . 其实如果我们很了解 FCL 的话我想这个实现起来也不难 ! 好了废话就不说了下面开始我们的创建工作 . 这次我们创建的是 Button 的数组 ( 也是 MSDN 上的例子 . 本文的原型以及代码均来自 MSDN, 如果想要了解更多的详细信息请参考 MSDN 的帮助 ), 首先我们用到了 ** CollectionBase ** ( 请参考 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionscollectionbaseclasstopic.asp ) 类 , 该类提供了一个抽象的强类型集合的基类 . 我们可以用它来实现我们的控件数组 . 首先我们需要建立一个从 CollectionBase 继承的类 , 我们称之为 ButtonArray. 用来做存放 Button 的容器 . 下面是实现的代码 :
namespace ButtonArrayProject{
using System;
///
1<summary>
2
3/// ButtonArray 的摘要说明。
4
5/// </summary>
public class ButtonArray : System.Collections.CollectionBase{
// 容器的一个引用 , 引用该控件数组所在的窗体的一个引用
private readonly System.Windows.Forms.Form HostForm;
// 添加一个新的按钮
public System.Windows.Forms.Button AddNewButton(){
// 创建一个新的按钮实例
System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();
// 将该新的控件添加到 (this) 列表中
this.List.Add(aButton);
// 将控件添加到父窗体的控件集合中
HostForm.Controls.Add(aButton);
// 设置该控件的属性
aButton.Top = Count * 25;
aButton.Left = 100;
// 用 Button 的 Tag 表示控件数组中的索引
aButton.Tag = this.Count;
aButton.Text = "Button " + this.Count.ToString();
// 添加一个事件响应
aButton.Click += new System.EventHandler(ClickHandler);
// 返回该新的控件
return aButton;
}
// 删除控件数组中的最后一个元素
public void Remove(){
// 检测该控件数组中是否有控件
if(this.Count > 0){
// 如果有则先从父容器中删除最后一个元素
// 注意 : 此处是通过索引访问
this.HostForm.Controls.Remove(this[this.Count-1]);
// 删除控件数组中的最后一个控件
this.List.RemoveAt(this.Count-1);
}
}
// 添加一个事件出来函数
public void ClickHandler(Object sender, System.EventArgs e) {
// 用 MessageBox 返回一条消息
System.Windows.Forms.MessageBox.Show(" 你点击的是 " +
((System.Windows.Forms.Button) sender).Tag.ToString());
}
// 带父窗体引用的构造函数
public ButtonArray(System.Windows.Forms.Form host){
// 为父窗体的引用赋值
this.HostForm = host;
// 添加一个默认的按钮
this.AddNewButton();
}
// 控件数组的索引可以利用索引访问 ( 只能得到 )
public System.Windows.Forms.Button this[int index]{
get{
return (System.Windows.Forms.Button)this.List[index];
}
}
}
}
为了测试程序我们添加一个 WinForm 的工程 . 在该工程的 Form1 中放入两个 Button 用来添加或者删除控件数组中的控件 . 同时我们还需要在 Form1 中声明一个 ButtonArray 的对象 . 在 Form1 的构造函数中实例该对象 , 代码如下 :
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ButtonArrayProject
{
public class Form1 : System.Windows.Forms.Form{
private ButtonArray btnArray ;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnRemove;
private System.ComponentModel.Container components = null ;
public Form1(){
InitializeComponent();
this .btnArray = new ButtonArray( this );
}
protected override void Dispose( bool disposing ){
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
#region Windows
private void InitializeComponent() {
this .btnAdd = new System.Windows.Forms.Button();
this .btnRemove = new System.Windows.Forms.Button();
this .SuspendLayout();
//
// btnAdd
//
this .btnAdd.Location = new System.Drawing.Point(352, 136);
this .btnAdd.Name = "btnAdd";
this .btnAdd.Size = new System.Drawing.Size(112, 23);
this .btnAdd.TabIndex = 0;
this .btnAdd.Text = "AddNewButton";
this .btnAdd.Click += new System.EventHandler( this .btnAdd_Click);
//
// btnRemove
//
this .btnRemove.Location = new System.Drawing.Point(352, 192);
this .btnRemove.Name = "btnRemove";
this .btnRemove.Size = new System.Drawing.Size(112, 23);
this .btnRemove.TabIndex = 1;
this .btnRemove.Text = "RemoveButton";
this .btnRemove.Click += new System.EventHandler( this .btnRemove_Click);
//
// Form1
//
this .AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this .ClientSize = new System.Drawing.Size(512, 381);
this .Controls.Add( this .btnRemove);
this .Controls.Add( this .btnAdd);
this .Name = "Form1";
this .Text = "Form1";
this .ResumeLayout( false );
}
#endregion
[STAThread]
static void Main () {
Application.Run( new Form1());
}
private void btnAdd_Click( object sender, System.EventArgs e) {
this .btnArray.AddNewButton();
// 这里就是通过索引访问的实例
this .btnArray[0].BackColor = Color.Blue;
}
private void btnRemove_Click( object sender, System.EventArgs e) {
this .btnArray.Remove();
}
}
}