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

后端 未结 12 1087
失恋的感觉
失恋的感觉 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: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().All(i => i.Selected = true);
    

    WinForms

    var selected = listBox.SelectedItems.Cast().ToArray();
    

提交回复
热议问题