您可以在触发事件时根据新的选中项值更改Label.Text
目标的属性。Label
ItemCheck
CheckedListBox
例子
假设您有 a Label
of name label1
、 a CheckedListBox
of namecheckedListBox1
和 a Form
of name Form1
,以下可能适用
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = "You are getting "; //Change the Text property of label1 to "You are getting "
checkedListBox1.ItemCheck += new ItemCheckEventHandler(checkedListBox1_ItemCheck); //Link the ItemCheck event of checkedListBox1 to checkedListBox1_ItemCheck; not required as long as you link the event through the designer
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked && e.CurrentValue == CheckState.Unchecked) //Continue if the new CheckState value of the item is changing to Checked
{
label1.Text += "a " + checkedListBox1.Items[e.Index].ToString() + ", "; //Append ("a " + the item's value + ", ") to the label1 Text property
}
else if (e.NewValue == CheckState.Unchecked && e.CurrentValue == CheckState.Checked) //Continue if the new CheckState value of the item is changing to Unchecked
{
label1.Text = label1.Text.Replace("a " + checkedListBox1.Items[e.Index].ToString() + ", ", ""); //Replace ("a " + the item's value + ", ") with an empty string and assign this value to the label1 Text property
}
}
}
样本输入
[x] Monitor
[x] Keyboard
[ ] Mouse
[x] Computer
样本输出
You are getting a Monitor, a Keyboard, a Computer,
谢谢,
我希望你觉得这有帮助:)