检查项目后会触发哪个 CheckedListBox 事件?
我有一个 CheckedListBox,我希望在检查项目后发生一个事件,以便我可以在新状态下使用 CheckedItems。
由于 ItemChecked 在 CheckedItems 更新之前被触发,因此它无法开箱即用。
当 CheckedItems 更新时,我可以使用什么类型的方法或事件来收到通知?
I have a CheckedListBox where I want an event after an item is checked so that I can use CheckedItems with the new state.
Since ItemChecked is fired before CheckedItems is updated it won't work out of the box.
What kind of method or event can I use to be notified when the CheckedItems is updated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
如果您还检查所单击的项目的新状态,则可以使用
ItemCheck
事件。这在事件参数中可用,如e.NewValue
。如果选中NewValue
,请在逻辑中包含当前项以及适当的集合:再举一个例子,要确定在(取消)选中此项后集合是否为空:
You can use the
ItemCheck
event, if you also check the new state of the item which is being clicked. This is available in the event args, ase.NewValue
. IfNewValue
is checked, include the current item along with the collection proper in your logic:As another example, to determine if the collection will be empty after this item is (un-)checked:
StackOverflow 上有很多相关的帖子...以及 Branimir 的 解决方案,这里还有两个更简单的解决方案:
< a href="https://stackoverflow.com/a/4454594/188926">ItemCheck 延迟执行(也此处):
使用 MouseUp 事件:
我更喜欢第一个选项,因为第二个选项会导致误报(即射击太频繁)。
There are lots of related StackOverflow posts on this... As well as Branimir's solution, here are two more simple ones:
Delayed execution on ItemCheck (also here):
Using the MouseUp event:
I prefer the first option, as the second would result in false positives (i.e. firing too often).
我尝试了这个并且它有效:
I tried this and it worked:
派生自
CheckedListBox
并实现Derive from
CheckedListBox
and implement尽管并不理想,但您可以使用传递给
ItemCheck
事件的参数来计算 CheckedItems。如果您查看 MSDN 上的示例,您可以确定新更改的项目是否已选中或未选中,这使您处于合适的位置来处理这些项目。您甚至可以创建一个在检查项目后触发的新事件,如果您愿意,这将准确地为您提供所需的内容。
Although not ideal, you can calculate the CheckedItems using the arguments that are passed through to the
ItemCheck
event. If you look at this example on MSDN, you can work out whether the newly changed item has been checked or unchecked, which leaves you in a suitable position to work with the items.You could even create a new event that fires after an item is checked, which would give you exactly what you wanted if you wished.
经过一些测试,我可以看到事件 SelectedIndexChanged 在事件 ItemCheck 之后触发。保留属性 CheckOnClick True
最佳编码
After some tests, I could see that the event SelectedIndexChanged is triggered after the event ItemCheck. Keep the property CheckOnClick True
Best coding
这可行,但不确定它有多优雅!
This works, not sure how elegant it is though!
我尝试了这个并且它有效:
通过列表的索引确定。
I tried this and it worked:
determine by index of the list.
不知道这是否适用,但我想使用清单框来过滤结果。因此,当用户选中和取消选中项目时,我希望列表显示\隐藏项目。
我遇到了一些问题,导致我写了这篇文章。只是想分享我是如何做到的,没有什么特别的。
” ,它可能仍然可以工作
注意:我有CheckOnClick = true,但如果没有我使用的事件是“SelectedIndexChanged”,
我使用的枚举是“.CheckedItems >”
这给出了我认为我们可以预期的结果。如此简化,归结为......
Don't know if this applies but I wanted to use a checklistbox to filter results. So as the user checked and unchecked items I wanted the list to show\hide items.
I was having some issues which led me to this post. Just wanted to share how I did it without anything special.
Note: I have CheckOnClick = true but it would probably still work without
The event I use is "SelectedIndexChanged"
the enumeration I use is ".CheckedItems"
This give the results I think we may expect. So simplified it comes down to ....
假设您想保留
ItemCheck
中的参数,但在模型更改后收到通知,它应该如下所示:其中
CheckedItemsChanged
可能是:Assuming you want to preserve the arguments from
ItemCheck
but get notified after the model was changed it should look like that:Where
CheckedItemsChanged
could be:你的意思是checkboxlist,而不是checklistbox?如果是这样,相关事件将为 SelectedIndexChanged。
例如VB中的处理程序定义头:
Do you mean checkboxlist, rather than checkedlistbox? If so, the the event concerned would be SelectedIndexChanged.
e.g. handler definition head in VB:
我决定不关心控件的感受,并处理该事件,就好像复选框更改确实已经完成一样。您所需要做的就是获取
CheckedIndices
列表,并使用ItemCheckEventArgs
对象中的信息将其调整为新状态。然后,您只需循环该列表并从控件的
Items
属性中检索指示的项目,即可获得CheckedItems
列表。结果在功能上与假设的所需情况相同,其中事件仅在更改后触发。
I decided to just not care about the control's feelings, and handle the event as if the checkbox change indeed went through already. All you need to do is take the
CheckedIndices
list, and use the information in theItemCheckEventArgs
object to adjust it to the new state.Then you can just loop over that list and retrieve the indicated items from the
Items
property of the control, and you have yourCheckedItems
list.The result is functionally identical to the hypothetical desired case where the event triggers only after the change.
VB.NET 版本的 Dunc 的回答 到
BeginInvoke
处理程序,以便检查该项目。VB.NET version of Dunc's answer to
BeginInvoke
a handler so the item is checked.如果像我一样,您尝试使用“选择”作为一个指示器(用户选择的项目),并且用户想要更改刻度,那么我找到了一个偷偷摸摸的解决方案。
在页面上创建一个计时器。例如,我的名为
tmrBan
,我有一个名为clbFTI
的CheckBoxList
。然后,为您的
CheckBoxList
创建一个 Click 事件。然后,为您的计时器创建一个刻度事件
,您将看到刻度闪烁,但可以调整计时器间隔,使其更适合您的情况。
If, like me, you were trying to use the Selection as one indicator (the item selected by the user), and the user wanting to changed the tick then I found a sneaky solution.
Create a timer on the page. For instance, mine is called
tmrBan
, and I have aCheckBoxList
calledclbFTI
.Then, create a Click Event for your
CheckBoxList
.Then, create a Tick Event for your timer
You will see a flicker of the tick, but play around with the timer Interval to make this better for your case.
我遇到了类似的问题:单击某个项目时,状态应从选中/未选中转换为相反。我在这里发布事件以及检查和更改:
I ran into a similar issue: On a click of an item, the state should be converted from either checked/ non-checked to opposite. Here i post the event and the check and change:
当您搜索 VB dotnet (.NET) 解决方案时会出现此线程,因此我将把我想到的解决方案放在这里(调用)。
编辑:使用 Invoke 一段时间后,我意识到这对于我的项目来说并不是一个很好的解决方案。它可以工作,但是操作顺序会被打乱。相反,我创建了一种方法来使用 SelectedItems.Count 以及新旧选择值来计算“固定”计数,以确定是否应该从总数中添加或减去 1。
This thread comes up when you search for VB dotnet (.NET) solutions, so I'm just going to put the solution I came up with here (Invoke).
EDIT: After using Invoke for a while, I realized it is not a great solution for my project. It works, but the order of operations will be thrown off. Instead, I created a method to calculate the 'fixed' count using the SelectedItems.Count along with the old and new selection values to determine if I should add or subtract one from the total.
我使用定时器来解决这个问题。通过 ItemCheck 事件启用计时器。在 Timer's Tick 事件中采取行动。
无论是通过单击鼠标还是按空格键检查该项目,这都有效。我们将利用这样一个事实:刚刚选中(或未选中)的项目始终是选定的项目。
计时器的间隔可以低至 1。当引发 Tick 事件时,将设置新的“已检查”状态。
此 VB.NET 代码展示了这个概念。您可以使用多种变体。您可能希望增加计时器的间隔,以允许用户在采取操作之前更改多个项目的检查状态。然后在 Tick 事件中,顺序传递列表中的所有项目或使用其 CheckedItems 集合来采取适当的操作。
这就是为什么我们首先在 ItemCheck 事件中禁用 Timer。禁用然后启用会导致间隔期重新开始。
I use a Timer to solve this problem. Enable the timer via the ItemCheck event. Take action in the Timer's Tick event.
This works whether the item is checked via a mouse click or by pressing the Space-Bar. We'll take advantage of the fact that the item just checked (or un-checked) is always the Selected Item.
The Timer's Interval can be as low as 1. By the time the Tick event is raised, the new Checked status will be set.
This VB.NET code shows the concept. There are many variations you can employ. You may want to increase the Timer's Interval to allow the user to change the check status on several items before taking action. Then in the Tick event, make a sequential pass of all the Items in the List or use its CheckedItems collection to take appropriate action.
That's why we first disable the Timer in the ItemCheck event. Disable then Enable causes the Interval period to re-start.
在正常行为中,当我们检查一项时,该项目的检查状态将在引发事件处理程序之前发生变化。
但是 CheckListBox 具有不同的行为:在项目的检查状态更改之前引发事件处理程序,这使得纠正我们的作业变得困难。
我认为,要解决这个问题,我们应该推迟事件处理程序。
In normal behaviour, when we check one item, the item's check state will change before the event handler is raised.
But a CheckListBox has a different behaviour: The event handler is raised before the check state of the item changes and that makes it difficult to correct our jobs.
In my opinion, to solve this problem, we should defer the event handler.