本例使用列表选择框控件,并向其中添加删除列表项,程序运行如下图所示。
程序代码如下。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace eg38_checklistBoxApp
{public partial class Form1 : Form{public Form1(){InitializeComponent();}void AddClick(object sender, EventArgs e){if(NewValue.Text.Trim()!=""){checkedListBox1.Items.Add(NewValue.Text);NewValue.Text="";}else{MessageBox.Show("请输入一个字符串!");}}void DeleteClick(object sender, EventArgs e){if(checkedListBox1.SelectedIndex!=-1){checkedListBox1.Items.RemoveAt(checkedListBox1.SelectedIndex);}else{MessageBox.Show("请选中待删除的项!");}}void ShowValueClick(object sender, EventArgs e){string selectedValues="以下值被选中:\n"+new string('-',48)+"\n";for(int i=0;i<checkedListBox1.CheckedItems.Count;i++){selectedValues+=checkedListBox1.CheckedItems[i].ToString()+"\n";}MessageBox.Show(selectedValues);}void CheckedListBox1SelectedIndexChanged(object sender, EventArgs e){OldValue.Text=checkedListBox1.Items[checkedListBox1.SelectedIndex].ToString();}}
}