You should use e.NewValue
instead of checkedListBox1.GetItemChecked(i)
. The reason being that checkedListBox1.GetItemChecked
is a cached state, because the ItemCheck
event occurs before the internal value is updated.
This'll work as you are expecting:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
...
}
else
{
...
}
}
Secondly, as to why the first time you click the checkbox, it doesn't react: that's because the CheckedListBox
object requires the item to be highlighted before changing the checkbox value through mouse clicks.
In order to achieve a similar effect, set checkedListBox1.CheckOnClick = true
. This will cause the checkbox to become checked whenever clicking on the checkbox or on the list item itself.