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

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

提交回复
热议问题