ListBox select all items

2019-01-26 03:45发布

问题:

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?

Thanks :)

回答1:

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.



回答2:

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:

   for (int i = 0; i < myListBox.Items.Count;i++)
   {
       myListBox.SetSelected(i, true);
   }


回答3:

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.

  [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));


回答4:

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.

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


回答5:

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.

myListBox.Visible = false;
for (int i = 0; i < myListBox.Items.Count;i++)
{
    myListBox.SetSelected(i, true);
}
myListBox.Visible = true;


回答6:

Within this Constructor, you need to enable the multi selection mode (MultiExtended) of the desired text box.

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 click to select the items individually.



回答7:

Select All is definetly available out of the box:

$("#ListBoxID option").prop("selected", true);


回答8:

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:

ASP.NET WebForms

var selected = listBox.Items.Cast<System.Web.UI.WebControls.ListItem>().All(i => i.Selected = true);

WinForms

var selected = listBox.SelectedItems.Cast<int>().ToArray();


回答9:

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]

lb.BeginUpdate();
lb.Select();
SendKeys.Send("{Home}");
SendKeys.Send("+{End}");
lb.EndUpdate();

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.



回答10:

private void Button_Click(object sender, RoutedEventArgs e)
    {

            listbox.SelectAll();

    }