【发布时间】:2011-12-04 03:49:22
【问题描述】:
如果没有选择或选择一个项目,我试图从 ListBox 中获取所有元素的列表,或者如果选择了超过 1 个,我正在尝试从 ListBox 中获取所有元素的列表。我已经写了这样的代码,但它没有编译:
ListBox.ObjectCollection listBoXElemetsCollection;
//loading of all/selected XMLs to the XPathDocList
if (listBoxXmlFilesReference.SelectedIndices.Count < 2)
{
listBoXElemetsCollection = new ListBox.ObjectCollection(listBoxXmlFilesReference);
}
else
{
listBoXElemetsCollection = new ListBox.SelectedObjectCollection(listBoxXmlFilesReference);
}
因此,要使这段代码正常工作,我需要使用类似 ListBox.SelectedObjectCollection listBoxSelectedElementsCollection; 的东西,这是我不想要的,因为我想在这样的 foreach 中使用它:
foreach (string fileName in listBoXElemetsCollection)
{
//...
}
【问题讨论】:
-
这是我找到的one good answer