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,含有控制項中目前選取項目的索引。 如果目前並未選取任何項目,將傳回空白的 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 屬性。