1. 显示一个 ListBox,其中在每项的左边显示一个复选框。
2. CheckedListBox 属性:
- CheckOnClick:true时,点击即选中,false,要点两下才选中。
- CheckedIndices:选中索引的集合,int集合。
- CheckedItems:选中项的集合,string集合。
3. CheckedListBox 方法:
- <bool> GetItemChecked(int index):返回指示指定项是否选中的值。
- <CheckState> GetItemCheckState(int index):返回指示指定项是否选中的值。(checked,unchecked,Indeterminate)
- SetItemChecked(int index, bool check):指定项是否选中。
- SetItemCheckState(int index, CheckState):指定项选中,三种状态。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Lists
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Add a tenth element to the CheckedListBox.
this.checkedListBoxPossibleValue.Items.Add("Ten");
}
private void buttonMove_Click(object sender, EventArgs e)
{
// Check if there are any checked items in the CheckedListBox.
if (this.checkedListBoxPossibleValue.CheckedItems.Count > 0)
{
// Clear the ListBox we'll move the selections to
this.listBoxSelected.Items.Clear();
foreach (int i in checkedListBoxPossibleValue.CheckedIndices)
{
MessageBox.Show(i.ToString());
}
// Loop through the CheckedItems collection of the CheckedListBox
// and add the items in the Selected ListBox
foreach (string item in this.checkedListBoxPossibleValue.CheckedItems)
{
this.listBoxSelected.Items.Add(item.ToString());
}
// Clear all the checks in the CheckedListBox
for (int i = 0; i < this.checkedListBoxPossibleValue.Items.Count; i++)
this.checkedListBoxPossibleValue.SetItemChecked(i, false);
}
}
}
}