控件数据及相应的事件处理

实例使用一组CheckBox 控件,实现事件的相应

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDI
{
    public partial class FormMDI2 : Form
    {
        public FormMDI2()
        {
            InitializeComponent();
            CheckBox[] cbox = new CheckBox[4];
            cbox[0] = checkBox1;
            cbox[1] = checkBox2;
            cbox[2] = checkBox3;
            cbox[3] = checkBox4;
            for (int i = 0; i < cbox.Length; i++)
            {
                cbox[i].CheckStateChanged += new EventHandler(cbox_Change);
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormMain frmain = (FormMain)this.Owner;
            //frmain.richTextBox1.Text += "MID2 窗口被调用\n";

        }

        private void cbox_Change(object sender, EventArgs e)
        {
            string cboxText = ((CheckBox)sender).Text;
            if(((CheckBox)sender).Checked)
                    MessageBox.Show(cboxText);
            
           
            //MessageBox.Show(sender.GetType().ToString());
             //switch (sender.ToString())
             //   case :
        }
    }
}

选中任何一个CheckBox 后,弹出提示内容

 

posted @ 2017-10-07 15:28  逯文杰  阅读(150)  评论(0编辑  收藏  举报