如何在选中复选框时选择列表框中的所有项目?
单击复选框时,我需要选择列表框中的所有项目。 是否可以使用一行代码选择列表框中的所有项目? 或者我是否必须循环遍历所有项目并将其中每一项的 selected 设置为 true ?
I need to select all items in a ListBox when a CheckBox is clicked. Is it possible to select all items in the ListBox using a single line of code? Or will I have to loop through all items and set selected to true for each one of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我使用 Mika 的解决方案,但是如果您有数千个项目,这可能会非常慢。 为了大幅提高速度,您可以短暂关闭可见性。 正如您可能怀疑的那样,列表框实际上不会在操作过程中消失,但在我的情况下,选择速度至少快 10 倍。
I use Mika's solution, however this can be very slow if you have thousands of items. For a massive speed increase you can turn off visibility briefly. The listbox will not actually disappear during the operation as you might suspect, but the selection occurs at least 10x faster in my case.
我想你必须在这里循环。 一次选择所有项目是一个非常具体(而且可能很少见)的用例,在这种情况下提供开箱即用的功能根本没有意义。 此外,无论如何,循环都只有两行代码。
I think you have to loop here. Selecting all items at once is a pretty specific (and probably rare) use case where it simply makes no sense to offer that functionality out of the box. Furthermore, the loop will be just two lines of code anyway.
在此构造函数中,您需要启用所需文本框的多选模式 (
MultiExtended
)。之后,使用循环选择所有内容:
我测试了它。 有用。 您还可以使用[CTRL/SHIFT]按钮+左键单击单独选择项目。
Within this Constructor, you need to enable the multi selection mode (
MultiExtended
) of the desired text box.After this, use a loop to select everything:
I tested it. It works. You can also use the [CTRL/SHIFT] button + left click to select the items individually.
据我所知,使用任何 .NET 方法选择大量项目都比直接进行 PInvoke 调用慢得多,将 LB_SETSEL 消息 (0x185) 传递给控件,并用一个标志指示是否要选择 (1) 或取消选择 (0) 以及魔法值 (-1),该值指示更改应应用于所有项目。
As far as I can tell, using any of the .NET methods to select a large number of items is far slower than making a direct PInvoke call, passing the LB_SETSEL message (0x185) to the control, with a flag indicating whether you want to Select (1) or Unselect (0) as well as the magic value (-1) which indicates that the change should apply to all items.
我见过很多(类似的)答案,它们在逻辑上都做同样的事情,我很困惑为什么它们都不适合我。 关键是将列表框的
SelectionMode
设置为SelectionMode.MultiSimple
。 它不适用于SelectionMode.MultiExtended
。 考虑在列表框中选择多个项目,您将选择模式设置为多种模式,并且大多数人都会选择传统的 MultiExtended 样式,这个答案应该有很大帮助。 而且你不是foreach
,而是for
。你实际上应该这样做:
或者
I have seen a number of (similar) answers all which does logically the same thing, and I was baffled why yet they all dont work for me. The key is setting listbox's
SelectionMode
toSelectionMode.MultiSimple
. It doesn't work withSelectionMode.MultiExtended
. Considering to select multiple items in a listbox, you will have selection mode set to multiple mode, and mostly people go for the conventionalMultiExtended
style, this answer should help a lot. And ya not aforeach
, butfor
.You should actually do this:
OR
就我而言,我有超过 10k 个项目,基本循环方法几乎需要一分钟才能完成。 使用 @DiogoNeves 答案并扩展它,我希望能够选择所有 (Ctrl+A) & 复制(Ctrl+C)。 我用两种方式处理了这个问题。 我使用 BeginUpdate() 和 EndUpdate() 来推迟绘制,但我还添加了直接复制全部 (Ctrl+Shift+C),它甚至不需要在复制之前选择项目。
In my case i had 10k+ items, the basic loop method was taking almost a minute to complete. Using @DiogoNeves answer and extending it i wanted to be able to select all (Ctrl+A) & copy (Ctrl+C). i handled this 2 ways. i used the BeginUpdate() and EndUpdate() to defer drawing but i also added a direct copy all (Ctrl+Shift+C) which doesn't even bother to select the items before copying.
如果你有很多(100+)个项目,这绝对不好,但比循环快得多:
选择列表框并模拟 [home] 和 [shift]+[end] 的按键输入
编辑:仅适用于 SelectionMode.MultiExtended 我猜
DoubleEDit:还要注意,这对于随后使用 lb.selecteditems 执行的代码可能太慢,但对于用户单击的[全选]按钮可能很有用。
this is absolutely not nice but much faster than a loop if you have many many (100+) items:
Select the Listbox and simulate key input of [home] and [shift]+[end]
EDIT: works with SelectionMode.MultiExtended only I guess
DoubleEDit: also be aware that this might be too slow for code being performed with lb.selecteditems afterwards, but it may be useful for an [Select All] button that the user will click.
全选绝对是开箱即用的:
Select All is definetly available out of the box:
我知道这个问题是用 .NET 2.0 标记的,但是如果您在 3.5+ 中可以使用 LINQ,您可以执行以下操作:
I know this question is tagged with .NET 2.0 but if you have LINQ available to you in 3.5+, you can do the following:
我将 nawfal 的想法添加到我已有的想法中,这也与“BeginUpdate”有关。 此外,正如用户所期望的那样,视图位置也被保持。 对我来说,这似乎现在解决了所有问题:
I added nawfal's idea to what I had already, which was also with 'BeginUpdate'. Additionaly the view position is maintained too, as the user would expect. For me this seems to solve all problems now:
事实上,
ListBox.Items
是一个普通对象集合,并返回普通的无类型对象,不能进行多选(默认情况下)。如果您想多选所有项目,那么这将起作用:
The fact is that
ListBox.Items
is a plain object collection and returns plain untyped objects, which cannot be multi-selected (by default).If you want to multi-select all items, then this will work: