ListBox.SelectedIndices プロパティ
ListBox 内で現在選択されているすべての項目の 0 から始まるインデックス番号を格納するコレクションを取得します。
Public ReadOnly Property SelectedIndices As _
ListBox.SelectedIndexCollection
[C#]
public ListBox.SelectedIndexCollection SelectedIndices {get;}
[C++]
public: __property ListBox.SelectedIndexCollection*
get_SelectedIndices();
[JScript]
public function get SelectedIndices() :
ListBox.SelectedIndexCollection;
プロパティ値
コントロール内で現在選択されている項目のインデックスを格納している ListBox.SelectedIndexCollection 。項目が現在選択されていない場合は、空の ListBox.SelectedIndexCollection が返されます。
解説
複数選択の ListBox の場合、このプロパティは ListBox 内で選択されているすべての項目のインデックスを格納するコレクションを返します。単一選択の ListBox の場合、このプロパティは、 ListBox 内で選択されている項目のインデックスだけを保持する、単一の要素を格納するコレクションを返します。コレクションの項目を操作する方法については、 ListBox.SelectedIndexCollection のトピックを参照してください。
ListBox クラスでは、選択されている項目をさまざまな方法で参照できます。単一選択の ListBox 内で現在選択されている項目のインデックス位置を取得するには、 SelectedIndices プロパティの代わりに、 SelectedIndex プロパティを使用できます。項目のインデックス位置ではなく、 ListBox 内で現在選択されている項目そのものを取得する場合は、 SelectedItem プロパティを使用します。また、複数選択の ListBox 内で選択されているすべての項目を取得する場合は、 SelectedItems プロパティを使用できます。
使用例
[Visual Basic, C#, C++] FindString メソッドを使用し、 ListBox の項目内から検索テキストのすべてのインスタンスを探し出す方法を次の例に示します。この例では、 ListBox 内のすべての項目の連続検索を実行する際の開始検索インデックスを指定できるバージョンの FindString メソッドを使用しています。この例では、再帰的な検索を防ぐために、項目のリストの最後に到達した後、リストの先頭から FindString メソッドが検索を開始するときを判定する方法についても示します。 ListBox に項目が見つかったら、 SetSelected メソッドを使用して選択されます。
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
[C#]
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);
}
}
[C++]
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->Item[0])
return;
}
// Select the item in the ListBox once it is found.
listBox1->SetSelected(x,true);
}
}while(x != -1);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
ListBox クラス | ListBox メンバ | System.Windows.Forms 名前空間 | SelectedIndex | SelectedItem | SelectedItems