C# 如何在选中的复选框上选择列表框中的所有项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/934241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I select all items in a listbox on checkbox checked?
提问by SO User
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?
单击 CheckBox 时,我需要选择 ListBox 中的所有项目。是否可以使用一行代码选择 ListBox 中的所有项目?或者我是否必须遍历所有项目并将它们中的每一项设置为 true ?
采纳答案by Joey
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.
我想你必须在这里循环。一次选择所有项目是一个非常具体(并且可能很少见)的用例,在这种情况下,开箱即用地提供该功能毫无意义。此外,无论如何,循环将只是两行代码。
回答by Mehdi LAMRANI
The fact is that ListBox.Items
is a plain object collection and returns plain untyped objects, which cannot be multi-selected (by default).
事实是这ListBox.Items
是一个普通对象集合并返回普通无类型对象,不能多选(默认情况下)。
If you want to multi-select all items, then this will work:
如果您想多选所有项目,那么这将起作用:
for (int i = 0; i < myListBox.Items.Count;i++)
{
myListBox.SetSelected(i, true);
}
回答by nawfal
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
to SelectionMode.MultiSimple
. It doesn't work with SelectionMode.MultiExtended
. Considering to select multiple items in a listbox, you will have selection mode set to multiple mode, and mostly people go for the conventional MultiExtended
style, this answer should help a lot. And ya not a foreach
, but for
.
我已经看到许多(类似的)答案,它们在逻辑上都做同样的事情,我很困惑为什么它们都不适合我。关键是将列表框设置SelectionMode
为SelectionMode.MultiSimple
. 它不适用于SelectionMode.MultiExtended
. 考虑在列表框中选择多个项目,您将选择模式设置为多个模式,并且大多数人选择传统MultiExtended
样式,这个答案应该有很大帮助。而且你不是一个foreach
,但是for
。
You should actually do this:
你实际上应该这样做:
lb.SelectionMode = SelectionMode.MultiSimple;
for (int i = 0; i < lb.Items.Count; i++)
lb.SetSelected(i, true);
lb.SelectionMode = //back to what you want
OR
或者
lb.SelectionMode = SelectionMode.MultiSimple;
for (int i = 0; i < lb.Items.Count; i++)
lb.SelectedIndices.Add(i);
lb.SelectionMode = //back to what you want
回答by Robin
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.
我使用 Mika 的解决方案,但是如果您有数千个项目,这可能会非常慢。要大幅提高速度,您可以暂时关闭能见度。列表框实际上并不会像您怀疑的那样在操作过程中消失,但在我的情况下,选择的速度至少要快 10 倍。
myListBox.Visible = false;
for (int i = 0; i < myListBox.Items.Count;i++)
{
myListBox.SetSelected(i, true);
}
myListBox.Visible = true;
回答by EricLaw
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.
据我所知,使用任何 .NET 方法来选择大量项目比直接调用 PInvoke 慢得多,将 LB_SETSEL 消息(0x185)传递给控件,并带有一个标志,指示您是否想要选择 (1) 或取消选择 (0) 以及表示更改应应用于所有项目的幻值 (-1)。
[DllImport("user32.dll", EntryPoint = "SendMessage")]
internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);
// Select All
SendMessage(listBox.Handle, 0x185, (IntPtr)1, (IntPtr)(-1));
// Unselect All
SendMessage(listBox.Handle, 0x185, (IntPtr)0, (IntPtr)(-1));
回答by Bartek
private void Button_Click(object sender, RoutedEventArgs e)
{
listbox.SelectAll();
}
回答by doomi
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]
如果您有很多(100+)个项目,这绝对不是很好,但比循环快得多:选择列表框并模拟 [home] 和 [shift]+[end] 的键输入
lb.BeginUpdate();
lb.Select();
SendKeys.Send("{Home}");
SendKeys.Send("+{End}");
lb.EndUpdate();
EDIT: works with SelectionMode.MultiExtended only I guess
编辑:仅适用于 SelectionMode.MultiExtended 我猜
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.
DoubleEDit:还要注意,这对于之后使用 lb.selecteditems 执行的代码来说可能太慢了,但它可能对用户将单击的 [Select All] 按钮有用。
回答by Rhonwen Van Druten
Within this Constructor, you need to enable the multi selection mode (MultiExtended
) of the desired text box.
在此构造函数中,您需要启用MultiExtended
所需文本框的多选模式 ( )。
public Form1()
{
InitializeComponent();
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox2.SelectionMode = SelectionMode.MultiExtended;
}
After this, use a loop to select everything:
在此之后,使用循环选择所有内容:
private void selectAll_Click(object sender, EventArgs e)
{
for (int val = 0; val < listBox1.Items.Count; val++)
{
listBox1.SetSelected(val, true);
}
}
I tested it. It works. You can also use the [CTRL/SHIFT] button + left clickto select the items individually.
我测试了它。有用。您也可以使用[CTRL/SHIFT] 按钮 + 左键单击来单独选择项目。
回答by jfatal
Select Allis definetly available out of the box:
全选是开箱即用的:
$("#ListBoxID option").prop("selected", true);
回答by fujiiface
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:
我知道这个问题是用 .NET 2.0 标记的,但是如果您在 3.5+ 中可以使用 LINQ,则可以执行以下操作:
ASP.NET WebForms
ASP.NET 网络表单
var selected = listBox.Items.Cast<System.Web.UI.WebControls.ListItem>().All(i => i.Selected = true);
WinForms
窗体
var selected = listBox.SelectedItems.Cast<int>().ToArray();