checkedListBox控件的使用简单示例

使用checkedListBox控件实现一个简单的权限选择功能

checkedListBox控件中的Items是一个集合,可以很方便的使用。

 

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.rbStudent = new System.Windows.Forms.RadioButton();
            this.clbStudent = new System.Windows.Forms.CheckedListBox();
            this.btnSave = new System.Windows.Forms.Button();
            this.tbxStudent = new System.Windows.Forms.TextBox();
            this.rbTeacher = new System.Windows.Forms.RadioButton();
            this.clbTeacher = new System.Windows.Forms.CheckedListBox();
            this.SuspendLayout();
            //
            // rbStudent
            //
            this.rbStudent.AutoSize = true;
            this.rbStudent.Location = new System.Drawing.Point(50, 56);
            this.rbStudent.Name = "rbStudent";
            this.rbStudent.Size = new System.Drawing.Size(71, 16);
            this.rbStudent.TabIndex = 0;
            this.rbStudent.Text = "学生信息";
            this.rbStudent.UseVisualStyleBackColor = true;
            this.rbStudent.CheckedChanged += new System.EventHandler(this.rbStudent_CheckedChanged);
            //
            // clbStudent
            //
            this.clbStudent.CheckOnClick = true;
            this.clbStudent.FormattingEnabled = true;
            this.clbStudent.Location = new System.Drawing.Point(187, 31);
            this.clbStudent.MultiColumn = true;
            this.clbStudent.Name = "clbStudent";
            this.clbStudent.Size = new System.Drawing.Size(120, 84);
            this.clbStudent.TabIndex = 1;
            //
            // btnSave
            //
            this.btnSave.Location = new System.Drawing.Point(338, 292);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(75, 23);
            this.btnSave.TabIndex = 2;
            this.btnSave.Text = "保存信息";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            //
            // tbxStudent
            //
            this.tbxStudent.Location = new System.Drawing.Point(50, 333);
            this.tbxStudent.Multiline = true;
            this.tbxStudent.Name = "tbxStudent";
            this.tbxStudent.Size = new System.Drawing.Size(363, 62);
            this.tbxStudent.TabIndex = 3;
            //
            // rbTeacher
            //
            this.rbTeacher.AutoSize = true;
            this.rbTeacher.Location = new System.Drawing.Point(50, 210);
            this.rbTeacher.Name = "rbTeacher";
            this.rbTeacher.Size = new System.Drawing.Size(71, 16);
            this.rbTeacher.TabIndex = 4;
            this.rbTeacher.TabStop = true;
            this.rbTeacher.Text = "教师信息";
            this.rbTeacher.UseVisualStyleBackColor = true;
            this.rbTeacher.CheckedChanged += new System.EventHandler(this.rbTeacher_CheckedChanged);
            //
            // clbTeacher
            //
            this.clbTeacher.CheckOnClick = true;
            this.clbTeacher.FormattingEnabled = true;
            this.clbTeacher.Location = new System.Drawing.Point(187, 177);
            this.clbTeacher.MultiColumn = true;
            this.clbTeacher.Name = "clbTeacher";
            this.clbTeacher.Size = new System.Drawing.Size(120, 84);
            this.clbTeacher.TabIndex = 5;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(466, 437);
            this.Controls.Add(this.clbTeacher);
            this.Controls.Add(this.rbTeacher);
            this.Controls.Add(this.tbxStudent);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.clbStudent);
            this.Controls.Add(this.rbStudent);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.RadioButton rbStudent;
        private System.Windows.Forms.CheckedListBox clbStudent;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.TextBox tbxStudent;
        private System.Windows.Forms.RadioButton rbTeacher;
        private System.Windows.Forms.CheckedListBox clbTeacher;

 

实际显示代码如下:

public partial class Form1 : Form
    {
        //用结构体来作数据集
        private struct  A
        {
            public string name;
            public string id;
            public A (string name ,string id)
            {
                this.name = name;
                this.id = id;
            }
            public override string ToString()
            {
                return name;
            }
        }
        //
        public Form1()
        {
            InitializeComponent();
        }

        private void rbStudent_CheckedChanged(object sender, EventArgs e)
        {
            this.clbStudent.Items.Clear();
            //string sno = System.Guid.NewGuid().ToString();
            //取全球唯一的字符码
            this.clbStudent.Items.Add(new A("增加学生", System.Guid.NewGuid().ToString()));
            this.clbStudent.Items.Add(new A("增加学生", System.Guid.NewGuid().ToString()));
            this.clbStudent.Items.Add(new A("增加学生", System.Guid.NewGuid().ToString()));
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.tbxStudent.Text = "&&";
            foreach (object o in this.clbStudent.CheckedItems)
            {
                this.tbxStudent.Text += "/n" + ((A)o).id;
            }
            this.tbxStudent.Text = this.tbxStudent.Text.Replace("&&/n","/n");
        }

        private void rbTeacher_CheckedChanged(object sender, EventArgs e)
        {
            this.clbTeacher.Items.Clear();
            this.clbTeacher.Items.Add(new A("增加教师", System.Guid.NewGuid().ToString()));
            this.clbTeacher.Items.Add(new A("增加教师", System.Guid.NewGuid().ToString()));
            this.clbTeacher.Items.Add(new A("增加教师", System.Guid.NewGuid().ToString()));
        }
    }

 

实现的功能的截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值