我有一个 ListBox
支持Multi-Select
我需要一种方法来检索所有选定的索引作为 List<int>
.
例如:
Listbox
{
item 1 - not selected
item 2 - selected
item 3 - selected
item 4 - not selected
}
所以选中的索引
List<int>
看起来像:List<int>() { 1, 2 };
最佳答案
尝试这个:
List<int> list = new List<int>();
foreach (var item in listBox1.SelectedItems)
{
list.Add(listBox1.Items.IndexOf(item));// Add selected indexes to the List<int>
}
或者使用 linq:
List<int> list = (from object obj in listBox1.SelectedItems
select listBox1.Items.IndexOf(obj)).ToList();
关于c# - 列表框(多选)选定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190848/