C# 如何在用户选中复选框时仅动态计算 CheckedListBox 中选中的项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13071125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count only checked items in CheckedListBox dynamically while the user is checking boxes?
提问by Indigo
I have a CheckedListBox control on my windows form application, which gives a list of items to select from. I want to count and show only checked (not selected) items while the user is still checking them. I mean count as you check them.
我的 Windows 窗体应用程序上有一个 CheckedListBox 控件,它提供了一个可供选择的项目列表。我想在用户仍在检查它们时计算并仅显示已选中(未选中)的项目。我的意思是当你检查它们时计数。
I have tried to use ItemCheck event and CheckedListBox.CheckedItems.Count but the problem is that it counts every other click even if the item is unchecked. When I check something it counts and if I unchecked it again, it counts that too.
我曾尝试使用 ItemCheck 事件和 CheckedListBox.CheckedItems.Count 但问题是,即使该项目未选中,它也会计算其他每次点击。当我检查某些东西时它很重要,如果我再次取消选中它,它也很重要。
I think it has to do with the remark given on MSDN "The check state is not updated until after the ItemCheck event occurs."I do not completely understand the problem here.
我认为这与 MSDN 上给出的评论“在 ItemCheck 事件发生之前不会更新检查状态”有关。我不完全理解这里的问题。
Thank you.
谢谢你。
采纳答案by Haedrian
The ItemCheckEventArgs parameter has the Property (NewValue) which tells you whether the change is a check, uncheck or neither.
ItemCheckEventArgs 参数具有属性 (NewValue),它告诉您更改是检查、取消检查还是两者都不是。
If CheckedItems.Count is not updated until after the event fires (which is what I'm understanding from that remark) - then you can add that count, and see whether the ItemChecckEventArgs is a check (+1) or an uncheck (-1) and you can get the correct total.
如果 CheckedItems.Count 直到事件触发后才更新(这是我从该评论中理解的) - 那么您可以添加该计数,并查看 ItemChecckEventArgs 是检查(+1)还是取消检查(-1 ),您可以获得正确的总数。
(Unless I'm understanding the remark wrongly, its very vague).
(除非我对这句话的理解有误,否则它非常含糊)。
回答by Michal Klouda
Add event handler for SelectedIndexChangedand get the count from CheckedItems.Count.
添加事件处理程序SelectedIndexChanged并从中获取计数CheckedItems.Count。
With ItemCheckyou don't have the actual value, but value before the last change is processed and you would need to adjust the count according to EventArgs as Haedrian proposed.
由于ItemCheck您没有实际值,但是在处理最后一次更改之前的值,您需要根据 Haedrian 建议的 EventArgs 调整计数。
回答by Indigo
It is not really a wise and intelligent work as a beginner but at the end solved my problem.
作为初学者,这并不是真正明智而聪明的工作,但最终解决了我的问题。
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.";
}
}
I would really appreciate comments on this code.
我非常感谢对此代码的评论。
回答by Shawn Kovac
Haedrian answered this correctly, but i think raw code goes a long ways too. So here's the C# code:
Haedrian 正确回答了这个问题,但我认为原始代码也有很长的路要走。所以这是 C# 代码:
private void CheckedListBox_ItemCheck(Object sender, ItemCheckEventArgs e)
{
int sCount = checkedListBox.CheckedItems.Count;
if (e.NewValue == CheckState.Checked ) { ++sCount; }
if (e.NewValue == CheckState.Unchecked) { --sCount; }
// now sCount is count of selected items in checkedListBox.
}
I had the same issue, and this question and answer gave me the solution. Thanks for the question and already existing answers!
我有同样的问题,这个问题和答案给了我解决方案。感谢您提出问题和已有答案!
回答by Sarath Avanavu
Since we won't get the updated value in the CheckedItems.Count immediately, we use e.NewValueto get the updated value to get whether the state is Checked or not.
由于我们不会立即e.NewValue获取CheckedItems.Count 中的更新值,因此我们使用获取更新值来获取状态是否为 Checked。
Declare a global variable for getting the count
声明一个获取计数的全局变量
int chkListCount = 0;
In the ItemCheckevent give the following code
在ItemCheck事件中给出以下代码
private void chkList_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
chkListCount++;
}
else if(e.NewValue == CheckState.Unchecked)
{
chkListCount--;
}
}
Its the simple logic to get the count of selected items
它是获取所选项目计数的简单逻辑
回答by TxCsharper
I know this has been answered long ago, but I found it easier to just handle the MouseUp and KeyUp events. The CheckedItems.Count property is accurate when those events are fired. Since they both do the same thing, I created a method to to the work and called that method from both event handlers.
我知道很久以前就已经回答了这个问题,但我发现处理 MouseUp 和 KeyUp 事件更容易。CheckedItems.Count 属性在触发这些事件时是准确的。由于他们都做同样的事情,我创建了一个方法来工作并从两个事件处理程序中调用该方法。
private void clbFolders_KeyUp(object sender, KeyEventArgs e) { Update(); }
private void clbFolders_MouseUp(object sender, MouseEventArgs e) { Update(); }
private void Update()
{
btnDelete.Enabled = clbFolders.CheckedItems.Count > 0;
}

