ListBox.SelectedIndexCollection.Count プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

コレクション内の項目の数。

このプロパティを使用すると、ListBox 内で選択されている項目の数を確認できます。この値は、コレクションの値をループするときに、ループの反復回数を指定する必要がある場合に使用できます。ListBox の SelectionMode プロパティが SelectionMode.MultiSimple または SelectionMode.MultiExtended のいずれかに設定されていない限り、このプロパティは、選択されている項目があるかどうかに応じて常に 0 または 1 の値を返します。

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
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 void FindAllOfMyString(String searchString) { // Set the SelectionMode property of the ListBox to // select multiple items. listBox1.set_SelectionMode(SelectionMode.MultiExtended); // Set our intial index variable to -1. int x = -1; // If the search string is empty exit. if (searchString.get_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.get_SelectedIndices().get_Count() > 0) { if (x == listBox1.get_SelectedIndices().get_Item(0)) { return; } } // Select the item in the ListBox once it is found. listBox1.SetSelected(x, true); } } while (x != -1); } } //FindAllOfMyString } //Form1

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からListBox.SelectedIndexCollection.Count プロパティを検索する場合は、下記のリンクをクリックしてください。

- ListBox.SelectedIndexCollection.Count プロパティのページへのリンク