ListBox.SelectedIndexCollection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示包含所选项的索引的 ListBox集合。
public: ref class ListBox::SelectedIndexCollection : System::Collections::IList
public class ListBox.SelectedIndexCollection : System.Collections.IList
type ListBox.SelectedIndexCollection = class
interface IList
interface ICollection
interface IEnumerable
Public Class ListBox.SelectedIndexCollection
Implements IList
- 继承
-
ListBox.SelectedIndexCollection
- 实现
示例
下面的示例演示如何使用 FindString 该方法在项 ListBox中搜索搜索文本的所有实例。 该示例使用方法的版本 FindString ,可用于指定起始搜索索引,以便从中连续搜索所有项 ListBox。 该示例还演示如何在 FindString 方法到达项列表底部后从列表顶部开始搜索,以防止递归搜索。 在找到项后 ListBox,将使用该方法 SetSelected 选择它们。
private:
void FindAllOfMyString( String^ searchString )
{
// Set the SelectionMode property of the ListBox to select multiple items.
listBox1->SelectionMode = SelectionMode::MultiExtended;
// Set our intial index variable to -1.
int x = -1;
// If the search string is empty exit.
if ( searchString->Length != 0 )
{
// Loop through and find each item that matches the search string.
do
{
// Retrieve the item based on the previous index found. Starts with -1 which searches start.
x = listBox1->FindString( searchString, x );
// If no item is found that matches exit.
if ( x != -1 )
{
// Since the FindString loops infinitely, determine if we found first item again and exit.
if ( listBox1->SelectedIndices->Count > 0 )
{
if ( x == listBox1->SelectedIndices[ 0 ] )
return;
}
// Select the item in the ListBox once it is found.
listBox1->SetSelected( x, true );
}
}
while ( x != -1 );
}
}
private void FindAllOfMyString(string searchString)
{
// Set the SelectionMode property of the ListBox to select multiple items.
listBox1.SelectionMode = SelectionMode.MultiExtended;
// Set our intial index variable to -1.
int x =-1;
// If the search string is empty exit.
if (searchString.Length != 0)
{
// Loop through and find each item that matches the search string.
do
{
// Retrieve the item based on the previous index found. Starts with -1 which searches start.
x = listBox1.FindString(searchString, x);
// If no item is found that matches exit.
if (x != -1)
{
// Since the FindString loops infinitely, determine if we found first item again and exit.
if (listBox1.SelectedIndices.Count > 0)
{
if(x == listBox1.SelectedIndices[0])
return;
}
// Select the item in the ListBox once it is found.
listBox1.SetSelected(x,true);
}
}while(x != -1);
}
}
Private Sub FindAllOfMyString(ByVal searchString As String)
' Set the SelectionMode property of the ListBox to select multiple items.
listBox1.SelectionMode = SelectionMode.MultiExtended
' Set our intial index variable to -1.
Dim x As Integer = -1
' If the search string is empty exit.
If searchString.Length <> 0 Then
' Loop through and find each item that matches the search string.
Do
' Retrieve the item based on the previous index found. Starts with -1 which searches start.
x = listBox1.FindString(searchString, x)
' If no item is found that matches exit.
If x <> -1 Then
' Since the FindString loops infinitely, determine if we found first item again and exit.
If ListBox1.SelectedIndices.Count > 0 Then
If x = ListBox1.SelectedIndices(0) Then
Return
End If
End If
' Select the item in the ListBox once it is found.
ListBox1.SetSelected(x, True)
End If
Loop While x <> -1
End If
End Sub
注解
该 ListBox.SelectedIndexCollection 类将索引存储在所选项中 ListBox。 存储在其中的 ListBox.SelectedIndexCollection 索引是类中的 ListBox.ObjectCollection 索引位置。 类
下表是一个示例,说明如何在ListBox.ObjectCollection示例中ListBox存储项目的项ListBox及其选择状态。
| Index | 项目 | ListBox 中的选择状态 |
|---|---|---|
| 0 | object1 | 未选中 |
| 1 | object2 | 已选择 |
| 2 | object3 | 未选中 |
| 3 | object4 | 已选择 |
| 4 | object5 | 已选择 |
下表根据 ListBox.ObjectCollection 上表中的示例演示了显示方式 ListBox.SelectedIndexCollection 。
| Index | ObjectCollection 中选定项的索引 |
|---|---|
| 0 | 1 |
| 1 | 3 |
| 2 | 4 |
可以使用此类的属性和方法对集合执行各种任务。 通过 Contains 该方法,可以确定类中的 ListBox.ObjectCollection 索引位置是否是存储在该类中的 ListBox.SelectedIndexCollection所选索引的成员。 知道该项位于集合中后,可以使用IndexOf该方法来确定存储集合ListBox.ObjectCollection中ListBox特定索引位置的位置。
构造函数
| 名称 | 说明 |
|---|---|
| ListBox.SelectedIndexCollection(ListBox) |
初始化 ListBox.SelectedIndexCollection 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| Count |
获取集合中项的数目。 |
| IsReadOnly |
获取一个值,该值指示集合是否为只读。 |
| Item[Int32] |
获取此集合中指定索引处的索引值。 |
方法
| 名称 | 说明 |
|---|---|
| Add(Int32) |
在指定的索引位置添加该 ListBox 位置。 |
| Clear() |
从集合中删除所有控件。 |
| Contains(Int32) |
确定指定的索引是否位于集合中。 |
| CopyTo(Array, Int32) |
将整个集合复制到数组内指定位置的现有数组中。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetEnumerator() |
返回用于循环访问所选索引集合的枚举器。 |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| IndexOf(Int32) |
从ListBox.SelectedIndexCollectionListBox.ObjectCollection指定的索引中ListBox返回索引中的索引。 |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| Remove(Int32) |
从集合中删除指定的控件。 |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
显式接口实现
| 名称 | 说明 |
|---|---|
| ICollection.IsSynchronized |
有关此成员的说明,请参阅 IsSynchronized。 |
| ICollection.SyncRoot |
有关此成员的说明,请参阅 SyncRoot。 |
| IList.Add(Object) |
有关此成员的说明,请参阅 Add(Object)。 |
| IList.Clear() |
有关此成员的说明,请参阅 Clear()。 |
| IList.Contains(Object) |
有关此成员的说明,请参阅 Contains(Object)。 |
| IList.IndexOf(Object) |
有关此成员的说明,请参阅 IndexOf(Object)。 |
| IList.Insert(Int32, Object) |
有关此成员的说明,请参阅 Insert(Int32, Object)。 |
| IList.IsFixedSize |
有关此成员的说明,请参阅 IsFixedSize。 |
| IList.Item[Int32] |
有关此成员的说明,请参阅 Item[Int32]。 |
| IList.Remove(Object) |
有关此成员的说明,请参阅 Remove(Object)。 |
| IList.RemoveAt(Int32) |
有关此成员的说明,请参阅 RemoveAt(Int32)。 |
扩展方法
| 名称 | 说明 |
|---|---|
| AsParallel(IEnumerable) |
启用查询的并行化。 |
| AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |
| Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定类型。 |
| OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |