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

后端 未结 12 1003
失恋的感觉
失恋的感觉 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 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.

提交回复
热议问题