CheckedListBox Action ItemCheck 删除项目?

发布于 2024-11-27 11:31:27 字数 695 浏览 3 评论 0原文

当取消选中选中的框时,我想删除该项目。问题是检查/取消检查似乎是在调用 ItemCheck 方法之后发生的。因此,当我删除一个弄乱 e.Index 的项目时,它会在我删除的项目之后检查/取消选中该项目,或者如果它是最后一个项目,则会抛出错误。

我发现了这个: Getting the ListView ItemCheck to stop! 其中有提示重置 e.NewValue 部分有效,但当我删除最后一项时它仍然会引发错误。

我没有简单地使用其中一个鼠标事件的原因是我希望键盘导航仍然可以以防万一。

这是我现在的代码。

private void checked_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Unchecked)
            {
                checked.Items.RemoveAt(e.Index);
                e.NewValue = CheckState.Checked;
            }
        }

感谢您的帮助

When a box that is checked is unchecked I want to remove that item. The trouble is check/unchecking seems to happen after the ItemCheck method is called. So, when I remove an item that messes up the e.Index so it does the check/uncheck on the item after the one I remove or throws an error if it is the last one.

I found this: Getting the ListView ItemCheck to stop! which has the tip of resetting the e.NewValue which partially works, but it still throws an error when I remove the last item.

The reason I have not simply used one of the mouse events is that I want keyboard navigation to still be possible just in case.

Here is the code I have now.

private void checked_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Unchecked)
            {
                checked.Items.RemoveAt(e.Index);
                e.NewValue = CheckState.Checked;
            }
        }

Thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2024-12-04 11:31:28

听起来您仍然遇到的唯一问题是删除最后一项后对 e.NewValue 的调用,对吗?如果是这种情况,请尝试以下操作:

private void checked_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Unchecked)
    {
        checked.Items.RemoveAt(e.Index);

        // If there are no items left, skip the CheckState.Checked call
        if (checked.Items.Count > 0)
        {
            e.NewValue = CheckState.Checked;
        }             
    }
} 

更新

好的 - 我让它工作了,尽管我不确定它有多漂亮。我使用了 SelectedIndexChanged 事件:

private void checked_SelectedIndexChanged(object sender, EventArgs e)
{

    CheckedListBox clb = (CheckedListBox)sender;
    int index = clb.SelectedIndex;

    // When you remove an item from the Items collection, it fires the SelectedIndexChanged
    // event again, with SelectedIndex = -1.  Hence the check for index != -1 first, 
    // to prevent an invalid selectedindex error
    if (index != -1 && clb.GetItemCheckState(index) == CheckState.Unchecked)
    {
        clb.Items.RemoveAt(index);
    }
}

我已经在 VS 2010 中测试了它并且它有效。

It sounds like the only problem you're still experiencing is the call to e.NewValue after you remove the last item, right? If that's the case, try this:

private void checked_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Unchecked)
    {
        checked.Items.RemoveAt(e.Index);

        // If there are no items left, skip the CheckState.Checked call
        if (checked.Items.Count > 0)
        {
            e.NewValue = CheckState.Checked;
        }             
    }
} 

UPDATE

Ok - I got it to work, though I'm not sure how pretty it is. I used the SelectedIndexChanged event:

private void checked_SelectedIndexChanged(object sender, EventArgs e)
{

    CheckedListBox clb = (CheckedListBox)sender;
    int index = clb.SelectedIndex;

    // When you remove an item from the Items collection, it fires the SelectedIndexChanged
    // event again, with SelectedIndex = -1.  Hence the check for index != -1 first, 
    // to prevent an invalid selectedindex error
    if (index != -1 && clb.GetItemCheckState(index) == CheckState.Unchecked)
    {
        clb.Items.RemoveAt(index);
    }
}

I've tested this in VS 2010 and it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文