新しいチェック項目の値に基づいてのイベントが発生したときにLabel.Text
、ターゲットのプロパティを変更できます。Label
ItemCheck
CheckedListBox
例
名前、名前、Label
名前のaがあると仮定すると、次のことが適用される場合があります。label1
CheckedListBox
checkedListBox1
Form
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,
ありがとう、
これがお役に立てば幸いです:)