作为初学者,这并不是一件真正明智和聪明的工作,但最终解决了我的问题。
private void CheckedListBox_ItemCheck(Object sender, ItemCheckEventArgs e)
{
int count = 0;
if (e.NewValue.ToString() == "Checked")
{
// first get all the checked items
foreach (object checkeditem in CheckedListBox.CheckedItems)
{
string checkItem = CheckedListBox.GetItemCheckState(CheckedListBox.Items.IndexOf(checkeditem)).ToString();
if (checkItem == "Checked")
{
count = count + 1;
}
}
// Now, below is the most important part considering the remark on MSDN
// "The check state is not updated until after the ItemCheck event occurs."
count = count + 1; // Plus 1 as the NewValue was Checked
labelCount.Text = "You have selected " + count + "Items.";
}
else if (e.NewValue.ToString() == "Unchecked")
{
// first get all the checked items
foreach (object checkeditem in CheckedListBox.CheckedItems)
{
string checkItem = CheckedListBox.GetItemCheckState(CheckedListBox.Items.IndexOf(checkeditem)).ToString();
if (checkItem == "Checked")
{
count = count + 1;
}
}
// Now, below is the most important part considering the remark on MSDN
// "The check state is not updated until after the ItemCheck event occurs."
count = count - 1; // minus 1 as the NewValue was Unchecked,
labelCount.Text = "You have Selected " + count + "Items.";
}
}
我非常感谢对此代码的评论。