checkedListBox应用实例

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

namespace _14._3._2
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.CheckedListBox checkedListBox1;
        private System.Windows.Forms.ListBox listBox1;
       // private System.ComponentModel.Container components;

        public Form1()
        {
            InitializeComponent();
            _InitializeComponent();
            // 设置CheckedListBox.控件中的项
            string[] myComponent = { "离散数学", "数据结构", "面向对象技术", 
                               "操作系统", "计算机网络", "计算机系统结构" };
            checkedListBox1.Items.AddRange(myComponent);

            // 设置CheckedListBox.控件的CheckOnClick属性
            checkedListBox1.CheckOnClick = true;

        }
        //初始化各个控件
        private void _InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
            this.listBox1 = new System.Windows.Forms.ListBox();

            this.textBox1.Location = new System.Drawing.Point(160, 15);
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

            this.button1.Enabled = false;
            this.button1.Location = new System.Drawing.Point(160, 65);
            this.button1.Size = new System.Drawing.Size(100, 30);
            this.button1.TabIndex = 1;
            this.button1.Text = "添加新课程";
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.button2.Enabled = false;
            this.button2.Location = new System.Drawing.Point(160, 115);
            this.button2.Size = new System.Drawing.Size(100, 30);
            this.button2.TabIndex = 2;
            this.button2.Text = "显示所选课程";
            this.button2.Click += new System.EventHandler(this.button2_Click);

            this.button3.Enabled = false;
            this.button3.Location = new System.Drawing.Point(160, 165);
            this.button3.Size = new System.Drawing.Size(100, 30);
            this.button3.TabIndex = 3;
            this.button3.Text = "保存所选课程";
            this.button3.Click += new System.EventHandler(this.button3_Click);

            this.checkedListBox1.Location = new System.Drawing.Point(15, 15);
            this.checkedListBox1.Size = new System.Drawing.Size(140, 200);
            this.checkedListBox1.TabIndex = 4;
            this.checkedListBox1.ItemCheck += new
                                    ItemCheckEventHandler(this.checkedListBox1_ItemCheck);

            this.listBox1.Location = new System.Drawing.Point(265, 15);
            this.listBox1.Size = new System.Drawing.Size(140, 200);
            this.listBox1.TabIndex = 5;

            this.ClientSize = new System.Drawing.Size(400, 230);
            // 将控件添加到属性窗体中
            this.Controls.AddRange(new System.Windows.Forms.Control[] {this.textBox1,
                                                                   this.button1,
                                                                   this.button2,
                                                                   this.button3,
                                                                   this.checkedListBox1,
                                                                   this.listBox1});
            this.Text = "课程设置";
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            if (textBox1.Text != "")
            {
                if (checkedListBox1.CheckedItems.Contains(textBox1.Text) == false)
                {
                    checkedListBox1.Items.Add(textBox1.Text, CheckState.Checked);
                }
                textBox1.Text = "";
            }

        }
        // 当textBox1里的文本内容发生变化时发生
        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            if (textBox1.Text == "")
            {
                button1.Enabled = false;
            }
            else
            {
                button1.Enabled = true;
            }

        }
        // 将CheckedListBox 控件里的各项移至listBox.控件
        private void button2_Click(object sender, System.EventArgs e)
        {
            listBox1.Items.Clear();
            button3.Enabled = false;
            for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {
                listBox1.Items.Add(checkedListBox1.CheckedItems[i]);
            }
            if (listBox1.Items.Count > 0)
            {
                button3.Enabled = true;
            }

        }
       //zql,修改checkedListBox1选项点击事件代码,原有代码错误
        private void checkedListBox1_ItemCheck(object sender,ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                button2.Enabled = true;
            }
            else 
            {
                if(checkListBox1.CheckedItems.Count == 1)
                {
                    button2.Enabled = false;
                }
            }
        }
        // 将选中项保存,清空ListBox控件和TextBox控件,取消CheckListBox中的所有的选中项
        private void button3_Click(object sender, System.EventArgs e)
        {
            listBox1.Items.Clear();
            IEnumerator enumerator;
            enumerator = checkedListBox1.CheckedIndices.GetEnumerator();
            int index;
            while (enumerator.MoveNext() != false)
            {
                index = (int)enumerator.Current;
                checkedListBox1.SetItemChecked(index, false);
            }
            button3.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

 

转载于:https://my.oschina.net/zhangqiliang/blog/1926755

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值