关于动态创建控件数组
我写一个程序,程序里会动态创建 RadioButton 及 CheckBox ,我利用集合的方式实现的.现在动态创建没有问题,我在集合中加了一个Click的事件函数,测试成功了.但是我需要是在引用这个集合的窗体中能够处理这个集合的事件.请高手指点,一下是部分代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace TelBill
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SerialPortConf());
}
}
/*
* 创建 RadioBox 控件集合类
* 当读取系统中的串口数后,用来创建接收数据的串口列表
* 接收串口列表控件为 RadioBox
* 存放RadioBox的容器限定为GroupBox.
*/
public class ReceivePortList : System.Collections.CollectionBase
{
// 引用动态添加 RadioBox 的 GroupBox 容器对象
private readonly System.Windows.Forms.GroupBox HostRadioBoxGroupBox;
// 初始化空间的坐标值
private int top = 24;
private int left = 24;
private int right = 132;
/* 构造函数
* 引用接收串口列表的 GroupBox
*/
public ReceivePortList(System.Windows.Forms.GroupBox RadioGroup)
{
// 将引用变量赋值,参数传递的是包含RadioBox的容器
this.HostRadioBoxGroupBox = RadioGroup;
}
// 添加一个新的 RadioBox 控件
public System.Windows.Forms.RadioButton AddNewRadioBox(string ctlLabel)
{
// 如果已经创建控件不到16个,则创建一个新的 RadioBox 对象
if (this.List.Count < 16) {
System.Windows.Forms.RadioButton NewRadio = new System.Windows.Forms.RadioButton();
NewRadio.Click += new System.EventHandler(ClickHandler);
this.List.Add(NewRadio);
this.HostRadioBoxGroupBox.Controls.Add(NewRadio);
// 设置控件属性
// 每行排两个控件
if (this.List.Count % 2 == 0)
{
NewRadio.Left = this.right;
NewRadio.Top = this.top;
NewRadio.Tag = ctlLabel;
this.top += 24;
}
else
{
NewRadio.Left = this.left;
NewRadio.Top = this.top;
NewRadio.Tag = ctlLabel;
}
NewRadio.Text = ctlLabel;
// 添加控件事件
// 返回创建的对象
return NewRadio;
} else {
// 设定最多支持16个串口,超过的不在设定,返回空值
return (System.Windows.Forms.RadioButton) this.List[this.List.Count];
}
}
public void ClickHandler(Object sender, System.EventArgs e)
{
// 用MessageBox返回一条消息
// MessageBox.Show(((System.Windows.Forms.RadioButton)sender).Tag.ToString());
}
public System.Windows.Forms.RadioButton this[int index] {
get {
return (System.Windows.Forms.RadioButton)this.List[index];
}
}
}
/*
* 创建 CheckBox 控件集合类
* 当读取系统中的串口数后,用来创建接收数据的串口列表
* 接收串口列表控件为 CheckBox
* 存放CheckBox的容器限定为GroupBox.
*/
public class TransmitPortList : System.Collections.CollectionBase
{
// 引用动态添加 CheckBox 的 GroupBox 容器对象
private readonly System.Windows.Forms.GroupBox HostCheckBoxGroupBox;
// 初始化空间的坐标值
private int top = 24;
private int left = 24;
private int right = 132;
/* 构造函数
* 引用转发串口列表的 GroupBox
*/
public TransmitPortList(System.Windows.Forms.GroupBox CheckGroup)
{
// 将引用变量赋值,参数传递的是包含CheckBox的容器
this.HostCheckBoxGroupBox = CheckGroup;
}
// 添加一个新的 CheckBox 控件
public System.Windows.Forms.CheckBox AddNewCheckBox(string ctlLabel)
{
// 创建一个新的 CheckBox 对象
System.Windows.Forms.CheckBox NewCheck = new System.Windows.Forms.CheckBox();
NewCheck.Click += new System.EventHandler(ClickHandler);
this.List.Add(NewCheck);
this.HostCheckBoxGroupBox.Controls.Add(NewCheck);
// 设置控件属性
// 每行排两个控件
if (this.List.Count % 2 == 0)
{
NewCheck.Left = this.right;
NewCheck.Top = this.top;
NewCheck.Tag = ctlLabel;
this.top += 24;
}
else
{
NewCheck.Left = this.left;
NewCheck.Top = this.top;
NewCheck.Tag = ctlLabel;
}
NewCheck.Text = ctlLabel;
// 添加控件事件
// 返回创建的对象
return NewCheck;
}
public void ClickHandler(Object sender, System.EventArgs e)
{
// 测试消息
MessageBox.Show(((System.Windows.Forms.CheckBox)sender).Tag.ToString());
}
public System.Windows.Forms.CheckBox this[int index]
{
get
{
return (System.Windows.Forms.CheckBox)this.List[index];
}
}
}
}
这个是创建控件集合的代码