首页 > 代码库 > CheckBox和richTextBox

CheckBox和richTextBox

namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            foreach (Control ctl in this.Controls)//遍历窗体中的控件            {                 if(ctl.GetType().Name =="CheckBox")//判断控件的类型是否是CheckBox                {CheckBox cbox=(CheckBox)ctl;//将遍历的控件强制转换                cbox.AutoEllipsis = true;//设置文本过长显示...
  //cbox.CheckAlign = ContentAlignment.MiddleCenter;千万不敢写这个选项,不然运行结果如下图
                 cbox.CheckAlign = ContentAlignment.BottomRight;                 cbox.Checked=false;                }            }        }        //选择篮球复选框        private void checkBox1_CheckedChanged(object sender, EventArgs e)        {            if (checkBox1.Checked)//判断checkBox1是否选中            {                if (richTextBox1.Text == "")                    richTextBox1.Text = checkBox1.Text;//为文本框赋值                else                    richTextBox1.Text +="," +checkBox1.Text;            }        }        //选择足球复选框        private void checkBox2_CheckedChanged(object sender, EventArgs e)        {            if (checkBox2.Checked)//判断checkBox2是否选中            {                if (richTextBox1.Text == "")                    richTextBox1.Text = checkBox2.Text;//为文本框赋值                else                    richTextBox1.Text += "," + checkBox2.Text;            }         }        //选择兵乓球复选框        private void checkBox3_CheckedChanged(object sender, EventArgs e)        {            if (checkBox3.Checked)//判断checkBox3是否选中            {                if (richTextBox1.Text == "")                    richTextBox1.Text = checkBox3.Text;//为文本框赋值                else                    richTextBox1.Text += "," + checkBox3.Text;            }        }    }}

 

CheckBox和richTextBox