ListBox.SelectedIndices 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得一個集合,包含所有目前選取 ListBox項目的零為基礎索引。
public:
property System::Windows::Forms::ListBox::SelectedIndexCollection ^ SelectedIndices { System::Windows::Forms::ListBox::SelectedIndexCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListBox.SelectedIndexCollection SelectedIndices { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndices : System.Windows.Forms.ListBox.SelectedIndexCollection
Public ReadOnly Property SelectedIndices As ListBox.SelectedIndexCollection
屬性值
ListBox.SelectedIndexCollection A 包含目前控制項目的索引。 若目前未選取任何項目,則會回傳一個空項目 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,此屬性回傳一個集合,包含所有在 中被選取 ListBox項目的索引。 對於單一選取 ListBox,此屬性會回傳一個集合,該集合包含唯一選取 ListBox項目索引的元素。 欲了解更多如何操作集合中的項目,請參見 ListBox.SelectedIndexCollection。
課程 ListBox 提供多種方式來參考所選項目。 你不必用該 SelectedIndices 屬性來取得當前選取項目 ListBox的索引位置,你可以使用該 SelectedIndex 屬性。 如果你想取得目前在 中選取 ListBox的項目,而不是項目的索引位置,請使用 屬性 SelectedItem 。 此外,如果你想在多選ListBox中取得所有指定項目,也可以使用該SelectedItems房產。