我写一个程序,程序里会动态创建 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];
            }
        }    }}
这个是创建控件集合的代码

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO.Ports;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;namespace TelBill
    {
            public partial class SerialPortConf : Form
        {
            // 创建串口对象集合
            static SerialPort curPort;
            private ReceivePortList ralist;
            private TransmitPortList trList;    
            public SerialPortConf()
            {
                InitializeComponent();
            }        private void MainForm_Load(object sender, EventArgs e)
            {            curPort = new SerialPort("COM1");
                this.ralist = new ReceivePortList(groupBoxReceive);
                this.trList = new TransmitPortList(groupBoxTransmit);            
                // 可以获得本机实际的串口数量
                int numPort = SerialPort.GetPortNames().Length;
                labelTip.Text += numPort.ToString();            if (numPort >= 16)
                {
                    // 最多支持16个串口
                    labelWork.Text += "16";
                }
                else {
                    labelWork.Text += numPort.ToString();
                }
                                        foreach (string s in SerialPort.GetPortNames())
                 {
                     this.ralist.AddNewRadioBox(s.ToString());
                     this.trList.AddNewCheckBox(s.ToString());
                    // this.ralist[].MouseClick += new MouseEventHandler(this.my_click);
                     this.ralist.ClickHandler += new EventHandler(this.my_click);
                     
                 }
                 //MessageBox.Show(SerialPort.InfiniteTimeout.ToString());
                 System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
        
                
                // 测试串口数据            curPort.Open();
                textBoxName.Text = curPort.PortName;            //  串口名称
                comboBoxSpeed.Text = curPort.BaudRate.ToString();       // 波特率
                comboBoxData.Text = curPort.DataBits.ToString();    // 数据位
                comboBoxParity.Text = curPort.Parity.ToString();    // 奇偶校验
                comboBoxStop.Text = curPort.StopBits.ToString();    // 停止位
                comboBoxControl.Text = curPort.Handshake.ToString();    // 握手协议,流控制,raw控制
                textBoxReceiveSize.Text = curPort.ReadBufferSize.ToString();    //接收缓冲区大小
                textBoxSendSize.Text = curPort.WriteBufferSize.ToString();      // 发送缓冲区大小
                textBoxReceiveTimeout.Text = curPort.ReadTimeout.ToString();    // 接收超时时间
                textBoxSendTimeout.Text = curPort.WriteTimeout.ToString();      //  发送超时时间
                textBoxEventNum.Text = curPort.ReceivedBytesThreshold.ToString();   // 触发DataReceived 事件的字符数            //checkBoxEOF.Checked  = curPort.                                     // 检测EOF
                checkBoxNULLChar.Checked = curPort.DiscardNull;                         // 是否忽略NULL 值
                checkBoxDTRState.Checked = curPort.DtrEnable;                       // DTR Enable 
                checkBoxRTSState.Checked = curPort.RtsEnable;                       // RTS Enable
                char tmpASCII = (char)curPort.ParityReplace;
                textBoxReplaceChar.Text = tmpASCII.ToString();        // 替换乱码字符            curPort.Close();
                // 结束测试串口数据        }        private void buttonExit_Click(object sender, EventArgs e)
            {
                this.Close();
                Application.Exit();
            }        private void buttonAdvance_Click(object sender, EventArgs e)
            {
                SerialPortAdvance PortAdvance = new SerialPortAdvance();
                PortAdvance.Show(this);
            }        private void buttonSave_Click(object sender, EventArgs e)
            {        }        private void my_click(object sender, EventArgs e) 
            {
                MessageBox.Show(sender.ToString());
            }      
        } //end class SerialPortConf
       
    }上面粗体的地方是不正确的,系统提示是:ClickHandle是个方法组,无法给他赋值.
    我该如何让写一个方法能够把集合中的所有控件的 click消息通过一个函数来解决,因为我每个创建的控件都增加了 tag标记,所以在处理时可以区分到底是谁.关键不知道该怎样实现?
    谢谢!!!
      

  2.   

    XXXX 具体控件=new XXXX()
    具体控件=this.ralist[i]
    再给具体控件加事件