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 = "目标列表中的所有项目已被移除";
}
}
}
ListBox,CheckedListBox,ComboBox控件的使用
最新推荐文章于 2021-09-11 18:13:58 发布