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

Dim instance As ListBox Dim value As Integer value = instance.SelectedIndex instance.SelectedIndex = value
[BindableAttribute(true)] public: virtual property int SelectedIndex { int get () override; void set (int value) override; }
/** @property */ public int get_SelectedIndex () /** @property */ public void set_SelectedIndex (int value)
public override function get SelectedIndex () : int public override function set SelectedIndex (value : int)
現在選択されている項目の 0 から始まるインデックス番号。項目が選択されていない場合は、値 -1 が返されます。


標準の ListBox の場合は、このプロパティを使用して、ListBox 内で選択されている項目のインデックスを確認できます。ListBox の SelectionMode プロパティが SelectionMode.MultiSimple または SelectionMode.MultiExtended のいずれかに設定され (複数選択の ListBox が指定されている)、リストで複数の項目が選択されている場合、このプロパティは、選択されている任意の項目のインデックスを返すことができます。
複数選択の ListBox 内で選択されているすべての項目のインデックスを格納するコレクションを取得するには、SelectedIndices プロパティを使用します。ListBox 内で現在選択されている項目を 1 つ取得する場合は、SelectedItem プロパティを使用します。また、SelectedItems プロパティを使用して、複数選択の ListBox 内で選択されているすべての項目を取得することもできます。

SelectedIndex プロパティと TopIndex プロパティを使用し、現在選択されている項目を ListBox の表示領域内の項目リストの先頭に移動する方法を次のコード例に示します。さらに、System.Windows.Forms.ListBox.ObjectCollection クラスの RemoveAt メソッドを使用して項目を削除する方法、ClearSelected メソッドを使用してすべての項目の選択を解除する方法についても示します。このコードは最初に、ListBox で現在選択されている項目をリストの先頭に移動します。このコードは次に、現在選択されている項目より前にある項目をすべて削除し、ListBox 内の選択をすべて解除します。この例では、項目を含む ListBox がフォームに追加されていて、いずれかの項目が ListBox で現在選択されている必要があります。
Private Sub RemoveTopItems() ' Determine if the currently selected item in the ListBox ' is the item displayed at the top in the ListBox. If listBox1.TopIndex <> listBox1.SelectedIndex Then ' Make the currently selected item the top item in the ListBox. listBox1.TopIndex = listBox1.SelectedIndex End If ' Remove all items before the top item in the ListBox. Dim x As Integer For x = listBox1.SelectedIndex - 1 To 0 Step -1 listBox1.Items.RemoveAt(x) Next x ' Clear all selections in the ListBox. listBox1.ClearSelected() End Sub 'RemoveTopItems
private void RemoveTopItems() { // Determine if the currently selected item in the ListBox // is the item displayed at the top in the ListBox. if (listBox1.TopIndex != listBox1.SelectedIndex) // Make the currently selected item the top item in the ListBox. listBox1.TopIndex = listBox1.SelectedIndex; // Remove all items before the top item in the ListBox. for (int x = (listBox1.SelectedIndex -1); x >= 0; x--) { listBox1.Items.RemoveAt(x); } // Clear all selections in the ListBox. listBox1.ClearSelected(); }
private: void RemoveTopItems() { // Determine if the currently selected item in the ListBox // is the item displayed at the top in the ListBox. if ( listBox1->TopIndex != listBox1->SelectedIndex ) // Make the currently selected item the top item in the ListBox. listBox1->TopIndex = listBox1->SelectedIndex; // Remove all items before the top item in the ListBox. for ( int x = (listBox1->SelectedIndex - 1); x >= 0; x-- ) { listBox1->Items->RemoveAt( x ); } // Clear all selections in the ListBox. listBox1->ClearSelected(); }
private void RemoveTopItems() { // Determine if the currently selected item in the ListBox // is the item displayed at the top in the ListBox. if (listBox1.get_TopIndex() != listBox1.get_SelectedIndex()) { // Make the currently selected item the top item in the ListBox. listBox1.set_TopIndex(listBox1.get_SelectedIndex()); } // Remove all items before the top item in the ListBox. for (int x = listBox1.get_SelectedIndex() - 1; x >= 0; x--) { listBox1.get_Items().RemoveAt(x); } // Clear all selections in the ListBox. listBox1.ClearSelected(); } //RemoveTopItems

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


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

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