時,基於新的檢查項目值打響CheckedListBox
的ItemCheck
事件你可以改變目標Label
的Label.Text
財產。
例
假設你有名字label1
的Label
,名checkedListBox1
的CheckedListBox
和名稱Form1
的Form
,以下情況可適用
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,
謝謝,
我希望對您有所幫助:)
這是的WinForms? WPF? – Blachshma
只是想澄清,你想觸發基於checkbox tick事件的列表? – bonCodigo
是的,Windows窗體應用程序。選項列表是在應用程序啓動時生成的,用戶按下其中描述的事件的按鈕,觸發按照選中選項出現在標籤中的文本。 – madman