Reputation: 850
I have a CheckedListBox in Winforms application containing Months as Items (like January, February, March etc.) I am trying to achieve the functionality that when any item is Checked,a message should pop up saying that it has been checked. While when the same item is unchecked, the message should say that it as been unchecked. I have tried the code below, but its not working as wanted. Currently when any item is checked or unchecked, the same set of message are being shown.
CheckOnClick=true
private void clbMonthly_SelectedIndexChanged(object sender, EventArgs e)
{
if (clbMonthly.GetItemChecked(1) == true)
{
MessageBox.Show("Item 1 checked");
}
else
{
MessageBox.Show("Item 1 unchecked");
}
//........
}
I hope you understand my question.Please suggest correct code in c#. Thanks in advance
Upvotes: 1
Views: 4793
Reputation: 17370
Try coding against the ItemCheck event:
Occurs when the checked state of an item changes.
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
string itemText = checkedListBox1.Items[e.Index].ToString();
if (e.NewValue == CheckState.Checked)
{
MessageBox.Show(itemText + " checked");
}
else
{
MessageBox.Show(itemText + " unchecked");
}
}
This is better than SelectedIndexChanged
because this one is triggered whenever the user selects a new item, vs an item getting checked/unchecked.
Upvotes: 8