CheckedListBox Action ItemCheck 删除项目?
当取消选中选中的框时,我想删除该项目。问题是检查/取消检查似乎是在调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您仍然遇到的唯一问题是删除最后一项后对
e.NewValue
的调用,对吗?如果是这种情况,请尝试以下操作:更新
好的 - 我让它工作了,尽管我不确定它有多漂亮。我使用了 SelectedIndexChanged 事件:
我已经在 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:UPDATE
Ok - I got it to work, though I'm not sure how pretty it is. I used the SelectedIndexChanged event:
I've tested this in VS 2010 and it works.