简体   繁体   English

列表框; 所选项目的数量

[英]listbox; number of selected items

While using listbox in c#, how can learn the count of selecteditems? 在c#中使用listbox时,如何学习选择项的数量?

Listbox items: A,B,C,D. 列表框项目:A,B,C,D。 For example, I select C and D. 例如,我选择C和D.

I want to make a loop in order to assign selecteditems. 我想制作一个循环以分配选定项目。

How can I achieve it? 我怎样才能实现它? How can I learn the number of selected item? 如何了解所选项目的数量?

Thank you 谢谢

也许你正在寻找这个listbox1.GetSelectedIndices().Count();

Use the following code: 使用以下代码:

This return integer: 这个返回整数:

 listBox.SelectedItems.Count

this will return the number as string : 这将返回数字作为字符串:

listBox.SelectedItems.Count.ToString()

You ought to be able to achieve this using something like so: 您应该能够使用类似的东西来实现这一目标:

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

The above is a way to get this using Linq (so you will need a reference to System.Linq ) but could easily be expanded to use a more primitive means such as a loop. 以上是使用Linq获取此内容的方法(因此您需要对System.Linq的引用),但可以轻松扩展为使用更原始的方法,如循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM