ListBox; number of items selected

When using listbox in C #, how do I know the number of selected items?

List items: A, B, C, D. For example, I select C and D.

I want to create a loop to assign selected items.

How can i achieve this? How to find out the number of selected items?

thanks

+6
source share
4 answers

Perhaps you are looking for this listbox1.GetSelectedIndices().Count();

+10
source

You should be able to achieve this using something like this:

 var count = (from item in listBox.Items where item.Selected select item).Count(); 

The above way to get this with Linq (so you'll need a reference to System.Linq ), but it can be easily extended to use more primitive tools such as a loop.

+1
source

Use the following code:

This is the returned integer:

  listBox.SelectedItems.Count 

this will return the number as a string:

 listBox.SelectedItems.Count.ToString() 
0
source
 int count = 0; foreach(ListItem item in this.ListBox1.Items) { if(item.Selected) { count++; } } int c = count; 
0
source

Source: https://habr.com/ru/post/895667/


All Articles