类生成动态控件的代码:
form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DAddControl
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnRemove;
ButtonArray MyControlArray;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
MyControlArray = new ButtonArray(this);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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(216, 0);
this.btnAdd.Name = "btnAdd";
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "Add Button";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(216, 240);
this.btnRemove.Name = "btnRemove";
this.btnRemove.TabIndex = 1;
this.btnRemove.Text = "Remove Button";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.btnAdd);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
//from www.w3sky.com
//调用MyControlArray的AddNewButton方法
MyControlArray.AddNewButton();
//改变Button 0的BackColor属性
MyControlArray[0].BackColor = System.Drawing.Color.Red;
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
MyControlArray.Remove();
}
}
}
ButtonArray.cs
using System;
using System.Windows.Forms;
namespace DAddControl
{
/// <summary>
/// ButtonArray 的摘要说明。
/// </summary>
public class ButtonArray
:System.Collections.CollectionBase
{
private readonly System.Windows.Forms.Form HostForm;
public ButtonArray()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public ButtonArray(System.Windows.Forms.Form host)
{
HostForm = host;
//this.AddNewButton();
}
public System.Windows.Forms.Button AddNewButton()
{
//为Button类建立新的实例
System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();
//将该按钮添加到集合的内部列表
this.List.Add(aButton);
//把控件集合中的按钮添加到被HostForm字段引用的窗体
HostForm.Controls.Add(aButton);
//设置该按钮对象的初始属性
aButton.Top = Count * 25;
aButton.Left = 100;
aButton.Tag = this.Count;
aButton.Text = "Button " + this.Count.ToString();
aButton.Click += new System.EventHandler(ClickHandler);
return aButton;
}
public System.Windows.Forms.Button this [int Index]
{
get
{
return (System.Windows.Forms.Button) this.List[Index];
}
}
public void Remove()
{
//检查以确保有按钮可以删除
if (this.Count > 0)
{
//from www.w3sky.com
// 从主窗体上的控件集合的数组按钮数组中删除最后一个
// 注意在访问数组时使用了默认属性
HostForm.Controls.Remove(this[this.Count -1]);
this.List.RemoveAt(this.Count -1);
}
}
public void ClickHandler(Object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("You have clicked button " +
((System.Windows.Forms.Button) sender).Tag.ToString());
}
}
}