ListBox,CheckedListBox,ComboBox控件的使用

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 ListBox_CheckedListBox_ComboBox控件的使用
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string[] chsNums;
        private string[] enNums;
        private string[] arNums;
        private void Form1_Load(object sender, EventArgs e)
        {
            chsNums = new string [] { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
            enNums = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
            arNums = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
            this.checkedListBoxSource.Items.AddRange(chsNums);
            this.comboBoxCategory.SelectedIndex = 0;
        }

        private void comboBoxCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            switch (cb.SelectedIndex)
            {
                case 0:
                    this.checkedListBoxSource.Items.Clear();
                    this.checkedListBoxSource.Items.AddRange(chsNums);
                    break;
                case 1:
                    this.checkedListBoxSource.Items.Clear();
                    this.checkedListBoxSource.Items.AddRange(enNums);
                    break;
                case 2:
                    this.checkedListBoxSource.Items.Clear();
                    this.checkedListBoxSource.Items.AddRange(arNums);
                    break;
            }
            this.listBoxDestination.Items.Clear();
            this.statusStrip1.Items[0].Text = "初始化操作已完成";
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            int count = this.checkedListBoxSource.CheckedItems.Count; //选中的条数
            if (this.checkedListBoxSource.Items.Count == 0) return;
            if (this.checkedListBoxSource.SelectedIndex == -1)
            {
                this.statusStrip1.Items[0].Text = "请在来源列表中选择要添加的项!";
                return;
            }
            //将左边列表中的项添加到右边
            for (int i = 0; i < count; i++)
                this.listBoxDestination.Items.Add(this.checkedListBoxSource.CheckedItems[i]);
            //将右边对应的项删除
            for (int i = count-1; i >= 0; i--)
                this.checkedListBoxSource.Items.Remove(this.checkedListBoxSource.CheckedItems[i]);
            this.statusStrip1.Items[0].Text = "选定的项已被移动到目标列表中";
        }

        private void buttonAddAll_Click(object sender, EventArgs e)
        {
            if (this.checkedListBoxSource.Items.Count == 0) return;
            this.listBoxDestination.Items.AddRange(this.checkedListBoxSource.Items);
            this.checkedListBoxSource.Items.Clear();
            this.statusStrip1.Items[0].Text = "来源列表中的所有的项已被移动到目标列表。";
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            int count = this.listBoxDestination.SelectedItems.Count;
            if (this.listBoxDestination.Items.Count == 0) return;
            if (this.listBoxDestination.SelectedIndex == -1)
            {
                this.statusStrip1.Items[0].Text = "请在目标列表中选择要移除的项!";
                return;
            }
            for (int i = 0; i < this.listBoxDestination.SelectedItems.Count; i++)
                this.checkedListBoxSource.Items.Add(listBoxDestination.SelectedItems[i]);
            for (int i = count - 1; i >= 0; i--)
                this.listBoxDestination.Items.Remove(listBoxDestination.SelectedItems[i]);
        }

        private void buttonRemoveAll_Click(object sender, EventArgs e)
        {
            if (this.listBoxDestination.Items.Count == 0) return;
            this.checkedListBoxSource.Items.AddRange(this.listBoxDestination.Items);
            this.listBoxDestination.Items.Clear();
            this.statusStrip1.Items[0].Text = "目标列表中的所有项目已被移除";
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值