我有这样的代码今天:检查ListViewItemCollection元素是否存在
MyListView.Items[index].Selected = true;
而且我想控制指标的值是否有效。如果该元素存在,如何在ListViewItemCollection中检查?
我有这样的代码今天:检查ListViewItemCollection元素是否存在
MyListView.Items[index].Selected = true;
而且我想控制指标的值是否有效。如果该元素存在,如何在ListViewItemCollection中检查?
如果您不希望抛出IndexOutOfRangeException,那么您必须先检查索引是否在集合的范围内,然后再尝试访问它。
这是可以做到这样的事:
if (index < MyListView.Items.Count()){
MyListView.Items[index].selected = true;
} else {
// handle the index being outside the collection
}
如果您想要特别小心,请检查'index> = 0'。 – 2014-09-26 10:51:06
确实。我认为这将在for循环内,其中index = 0; index
你能使用'MyListView.Items.Count'工作? – 2014-09-26 10:36:28