Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当用户选中复选框时,如何仅动态统计CheckedListBox中的选中项?_C#_Checkedlistbox - Fatal编程技术网

C# 当用户选中复选框时,如何仅动态统计CheckedListBox中的选中项?

C# 当用户选中复选框时,如何仅动态统计CheckedListBox中的选中项?,c#,checkedlistbox,C#,Checkedlistbox,我的windows窗体应用程序上有一个CheckedListBox控件,它提供了一个可供选择的项目列表。 我想在用户仍在检查时,只计算并显示选中的项目,而不显示选中的项目。我的意思是当你检查它们时数一数 我曾尝试使用ItemCheck事件和CheckedListBox.CheckedItems.Count,但问题是,即使项目未选中,它也会每隔一次单击计数一次。当我检查某些东西时,它会计数,如果我再次检查它,它也会计数 我认为这与MSDN上的评论有关,我不完全理解这里的问题 多谢各位 为Selec

我的windows窗体应用程序上有一个CheckedListBox控件,它提供了一个可供选择的项目列表。 我想在用户仍在检查时,只计算并显示选中的项目,而不显示选中的项目。我的意思是当你检查它们时数一数

我曾尝试使用ItemCheck事件和CheckedListBox.CheckedItems.Count,但问题是,即使项目未选中,它也会每隔一次单击计数一次。当我检查某些东西时,它会计数,如果我再次检查它,它也会计数

我认为这与MSDN上的评论有关,我不完全理解这里的问题

多谢各位

为SelectedIndexChanged添加事件处理程序,并从CheckedItems.count获取计数


使用ItemCheck时,您没有实际值,但在处理最后一次更改之前有值,您需要根据Haedrian建议的EventArgs调整计数。

ItemCheckEventArgs参数具有属性NewValue,它告诉您更改是检查,还是取消选中

如果CheckedItems.Count直到事件触发后才更新,这是我从该评论中了解到的,那么您可以添加该计数,并查看ItemChecckEventArgs是check+1还是uncheck-1,您可以得到正确的总数


除非我对这句话理解错误,否则它是非常模糊的。

作为一个初学者,这不是一个真正明智的工作,但最终解决了我的问题

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.";
        }
    }  

我非常感谢您对这段代码的评论

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.
}

我也有同样的问题,这个问题和答案给了我答案。谢谢你的问题和已有的答案

由于我们不会立即在CheckedItems.Count中获取更新的值,因此我们使用e.NewValue获取更新的值以获取状态是否已检查

声明用于获取计数的全局变量

int chkListCount = 0;
在ItemCheck事件中,给出以下代码

private void chkList_ItemCheck(object sender, ItemCheckEventArgs e)
{
     if (e.NewValue == CheckState.Checked)
     {
         chkListCount++;
     }
     else if(e.NewValue == CheckState.Unchecked)
     {
         chkListCount--;
     }
}

获取所选项目的计数是一个简单的逻辑

我知道这在很久以前就已经得到了回答,但我发现处理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;
    }

非常感谢,你理解正确。在我的代码中进行了一些更新之后,它就工作了。我将把它作为一个帖子贴在下面:再次感谢。谢谢,我找到了解决办法,虽然不是很明智,但效果很好。我将在下面发布。最好不要转换为字符串,然后测试该字符串值,如果不使用.ToString方法也可以这样做的话。循环检查CheckedItems中的每个对象是不必要的,因为您可以从CheckedListBox.CheckedItems.count属性获取计数。加上.CheckedItems中的每一项都将被选中,因为您已经只在CheckedListBox的选中项中循环。您所需要做的只是获取计数,然后将该计数值增加或减少1。它只需要三行普通代码。请看我用这三行代码分别给出的答案。