CheckedListBox类似于ListBox和CheckBox控件的综合,可以在ListBox中选择具体内容.
1. 选中元素遍历
public partial class CheckedListBox : Form { public CheckedListBox() { InitializeComponent(); addItems(); } private void addItems() { this.checkedListBox1.Items.Add("ItemA"); this.checkedListBox1.Items.AddRange(new String[] { "ItemB", "ItemC" }); } private void selectedItem() { if (this.checkedListBox1.CheckedItems.Count!=0) //如果选中 { string checkedItem = ""; for (int i = 0; i < this.checkedListBox1.CheckedItems.Count;i++ ) { checkedItem += "选中项" + (i + 1).ToString() + ": " + this.checkedListBox1.CheckedItems[i].ToString() + "\r\n"; } MessageBox.Show(checkedItem); } } private void button1_Click(object sender, EventArgs e) { selectedItem(); } }
另外两个属性CheckOnClick是自动选择模式,当点击ItemB(比如)的时候会自动选上.如果自动选择模式为false,则点击ItemB两次或者点击一次再点击前面的方框才选上.
SelectionMode属性对于如何选择的方式,支持四种分别是MultiExtend, MultiSimple, One,None的枚举类型.当使用前两个会报错
当使用One的时候和默认的一样,使用None则选不上对应的项
2. 应用
public partial class CheckedListBox : Form { public CheckedListBox() { InitializeComponent(); addItems(); setCheckOnClick(); } private void addItems() { this.checkedListBox1.Items.AddRange(new String[] { "离散数学","数据结构","面向对象技术", "操作系统","计算机网络","计算机系统结构"}); } private void selectedItem() { if (this.checkedListBox1.CheckedItems.Count!=0) { string checkedItem = ""; for (int i = 0; i < this.checkedListBox1.CheckedItems.Count;i++ ) { checkedItem += "选中项" + (i + 1).ToString() + ": " + this.checkedListBox1.CheckedItems[i].ToString() + "\r\n"; } MessageBox.Show(checkedItem); } } private void setCheckOnClick() { this.checkedListBox1.CheckOnClick = true; } private void button1_Click(object sender, EventArgs e) { selectedItem(); } private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox1.Text == "") { button1.Enabled = false; } else { button1.Enabled = true; } } private void button1_Click_1(object sender, EventArgs e) { if (this.textBox1.Text != null) { if (!this.checkedListBox1.CheckedItems.Contains(this.textBox1.Text)) { //如果不包含文本框中的内容 //则添加且默认选中 this.checkedListBox1.Items.Add(this.textBox1.Text, CheckState.Checked); } this.textBox1.Text = ""; } } private void button2_Click(object sender, EventArgs e) { this.listBox1.Items.Clear();//清空listBox this.button3.Enabled = false; //将选中项添加到listBox中 for (int i = 0; i < this.checkedListBox1.CheckedItems.Count;i++ ) { this.listBox1.Items.Add(this.checkedListBox1.CheckedItems[i]); } //当listbox中有项的时候恢复button3 if (this.listBox1.Items.Count > 0) { this.button3.Enabled = true; } } private void button3_Click(object sender, EventArgs e) { //将选中项进行保存,取消选中项 this.listBox1.Items.Clear(); int index; //IEnumerator是所有非泛型枚举的基接口 System.Collections.IEnumerator enumerator = this.checkedListBox1.CheckedIndices.GetEnumerator(); while (enumerator.MoveNext()!=false) { index= (int)enumerator.Current; this.checkedListBox1.SetItemChecked(index, false); } button3.Enabled = false; } private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { if (e.NewValue == CheckState.Unchecked) { if (checkedListBox1.CheckedItems.Count == 1)//没有选中项 { button2.Enabled = false; } } else { button2.Enabled = true; } } }
designer.cs
扫描二维码关注公众号,回复:
959179 查看本文章
private void InitializeComponent() { this.checkedListBox1 = new System.Windows.Forms.CheckedListBox(); 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.listBox1 = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // checkedListBox1 // this.checkedListBox1.FormattingEnabled = true; this.checkedListBox1.Location = new System.Drawing.Point(15, 15); this.checkedListBox1.Name = "checkedListBox1"; this.checkedListBox1.Size = new System.Drawing.Size(140, 196); this.checkedListBox1.TabIndex = 4; this.checkedListBox1.ItemCheck +=new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox1_ItemCheck); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(160, 15); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 21); this.textBox1.TabIndex = 0; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // button1 // this.button1.Enabled = false; this.button1.Location = new System.Drawing.Point(160, 65); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(100, 30); this.button1.TabIndex = 1; this.button1.Text = "添加新课程"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click_1); // // button2 // this.button2.Enabled = false; this.button2.Location = new System.Drawing.Point(160, 115); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(100, 30); this.button2.TabIndex = 2; this.button2.Text = "显示所选课程"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Enabled = false; this.button3.Location = new System.Drawing.Point(160, 165); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(100, 30); this.button3.TabIndex = 3; this.button3.Text = "保存所选课程"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // listBox1 // this.listBox1.FormattingEnabled = true; this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(265, 15); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(140, 196); this.listBox1.TabIndex = 5; // // CheckedListBox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(442, 266); this.Controls.Add(this.listBox1); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Controls.Add(this.checkedListBox1); this.Name = "CheckedListBox"; this.Text = "课程设置"; this.ResumeLayout(false); this.PerformLayout(); }
结果