c# 自定义多选框checkbox

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

namespace ComboBoxEx
{
    public partial class UserControl1 : UserControl
    {
        public static int checkSize = 20;
        public static string[] checkBoxList = new string[checkSize];
        Panel panel1;
        CheckBox selectAll = new CheckBox();

        public UserControl1()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
//
            for (int j = 0; j < checkBoxList.Length; j++)
            {
                checkBoxList[j] = "java" + j;
            }

            // 控件隱藏
            txtExtension.Visible = false;
            btnSelect.Visible = false;

            // 控件大小設置
            Size = new System.Drawing.Size(450, 70 + checkBoxList.Length / 5 * 35);

            //
            // panel1
            //
            panel1.SuspendLayout();
            panel1.BackColor = System.Drawing.SystemColors.ButtonHighlight;
            panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;   
            panel1.Location = new System.Drawing.Point(0, 0);
            panel1.Name = "panel1";
            panel1.Size = new System.Drawing.Size(450, 70 + checkBoxList.Length / 5 * 35);
            panel1.Visible = true; 

            // 動態生成Checkbox
            for (int i = 0; i < checkBoxList.Length; i++)
            {
                CheckBox ckb = new CheckBox();
                ckb.Text = checkBoxList[i];
                ckb.AutoSize = true;
                ckb.Location = new System.Drawing.Point(30 + 70*(i%5), 20 + i/5*35);
                ckb.Name = checkBoxList[i];
                ckb.Size = new System.Drawing.Size(80, 16);
                ckb.UseVisualStyleBackColor = true;
                panel1.Controls.Add(ckb);
            }

            //
            // selectAll
            //
            selectAll = new CheckBox();
            selectAll.Text = "SelectAll";
            selectAll.CheckedChanged += new EventHandler(selectAll_CheckedChanged);
            selectAll.AutoSize = true;
            selectAll.Location = new System.Drawing.Point(120, 35 + checkBoxList.Length / 5 * 35);
            panel1.Controls.Add(selectAll);

            //
            // btnOK
            //
            Button btnOK = new System.Windows.Forms.Button();
            btnOK.Name = "btnOK";
            btnOK.Size = new System.Drawing.Size(40, 25);
            btnOK.TabIndex = 8;
            btnOK.Text = "OK";
            btnOK.UseVisualStyleBackColor = true;
            btnOK.Click += new System.EventHandler(btnOK_Click);
            btnOK.Location = new System.Drawing.Point(200, 30 + checkBoxList.Length / 5 * 35);
            panel1.Controls.Add(btnOK);
 
            Controls.Add(panel1);
            panel1.ResumeLayout(false);
            panel1.PerformLayout();
        }

        /// <summary>
        /// OKボタン按下
        /// </summary>
        private void UserControl1_Load(object sender, EventArgs e)
        {
            this.Size = new System.Drawing.Size(162, 20);
            this.panel1 = new System.Windows.Forms.Panel();
            panel1.Visible = false;
        }

        /// <summary>
        /// 控件初期化
        /// </summary>
        private void btnOK_Click(object sender, EventArgs e)
        {
            // 控件大小設置
            Size = new System.Drawing.Size(162, 20);

            // 控件顯示
            txtExtension.Visible = true;
            btnSelect.Visible = true;

            // 全選
            CheckBox all = sender as CheckBox;
            string extension = "";
            bool isFirst = true;

            // 文本框清空
            txtExtension.Text = "";
            foreach (Control ctl in panel1.Controls)
            {
                if (ctl is CheckBox)
                {
                    CheckBox chk = ctl as CheckBox;
                    if (chk.Checked)
                    {
                        if (!isFirst)
                        {
                            extension = extension + ',';
                        }
                        extension = extension + chk.Name;
                        isFirst = false;
                    }
                }
            }
            // 文本框賦直
            txtExtension.Text = extension;
        }

        /// <summary>
        /// 全選Click
        /// </summary>
        void selectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox all = sender as CheckBox;
            foreach (Control ctl in panel1.Controls)
            {
                if (ctl is CheckBox)
                {
                    CheckBox chk = ctl as CheckBox;
                    chk.Checked = all.Checked;
                }
            }
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值