How do I select all items in a listbox on checkbox checked?

后端 未结 12 1004
失恋的感觉
失恋的感觉 2020-12-17 08:31

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 thr

相关标签:
12条回答
  • 2020-12-17 08:48

    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.

    0 讨论(0)
  • 2020-12-17 08:50

    Select All is definetly available out of the box:

    $("#ListBoxID option").prop("selected", true);
    
    0 讨论(0)
  • 2020-12-17 08:54

    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;
    
    0 讨论(0)
  • 2020-12-17 08:55

    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.

    private static void HandleListBoxKeyEvents(object sender, KeyEventArgs e)
    {
        var lb = sender as ListBox;
        // if copy
        if (e.Control && e.KeyCode == Keys.C)
        {
            // if shift is also down, copy everything!
            var itemstocopy = e.Shift ? lb.Items.Cast<object>() : lb.SelectedItems.Cast<object>();
            // build clipboard buffer
            var copy_buffer = new StringBuilder();
            foreach (object item in itemstocopy)
                copy_buffer.AppendLine(item?.ToString());
            if (copy_buffer.Length > 0)
                Clipboard.SetText(copy_buffer.ToString());
        }
        // if select all
        else if (e.Control && e.KeyCode == Keys.A)
        {
            lb.BeginUpdate();
            for (var i = 0; i < lb.Items.Count; i++)
                lb.SetSelected(i, true);
            lb.EndUpdate();
        }
    }
    
    0 讨论(0)
  • 2020-12-17 08:56

    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
    
    0 讨论(0)
  • 2020-12-17 08:58

    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);
       }
    
    0 讨论(0)
提交回复
热议问题