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

后端 未结 12 1005
失恋的感觉
失恋的感觉 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:58

    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));
    
    0 讨论(0)
  • 2020-12-17 09:01
    private void Button_Click(object sender, RoutedEventArgs e)
        {
    
                listbox.SelectAll();
    
        }
    
    0 讨论(0)
  • 2020-12-17 09:02

    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.

    0 讨论(0)
  • 2020-12-17 09:03

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

    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:

    public void SelectAll()
    {
        bool prevBusy = MouseHelper.IsBusy;
        MouseHelper.IsBusy = true;
        int topIndex = TopIndex;
    
        // BUG: In 'SelectionMode.MultiExtended' the box gets crazy
        SelectionMode previousMode = this.SelectionMode;
        this.SelectionMode = SelectionMode.MultiSimple;
    
        this.BeginUpdate();
    
        for (int i = 0; i < Items.Count; i++)
        {
            SelectedIndices.Add(i);
        }
    
        this.EndUpdate();
        this.SelectionMode = previousMode;
    
        TopIndex = topIndex;
        MouseHelper.IsBusy = prevBusy;
    }
    
    0 讨论(0)
  • 2020-12-17 09:07

    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.

    0 讨论(0)
提交回复
热议问题