次の方法で共有


ListBox.ObjectCollection.RemoveAt メソッド

このメソッドは、.NET Framework インフラストラクチャをサポートします。独自に作成したコードから直接使用するためのものではありません。

コレクション内の指定されたインデックスにある項目を削除します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

構文

'宣言
Public Sub RemoveAt ( _
    index As Integer _
)
'使用
Dim instance As ObjectCollection
Dim index As Integer

instance.RemoveAt(index)
public void RemoveAt (
    int index
)
public:
virtual void RemoveAt (
    int index
) sealed
public final void RemoveAt (
    int index
)
public final function RemoveAt (
    index : int
)

パラメータ

  • index
    削除する項目の 0 から始まるインデックス。

例外

例外の種類 条件

ArgumentOutOfRangeException

index パラメータの値がゼロ未満か、ListBox.ObjectCollection クラスの Count プロパティの値以上です。

解説

項目をリストから削除すると、リスト内にある後続項目のインデックスが変わります。削除される項目に関するすべての情報が削除されます。このメソッドを使用してリストから特定の項目を削除するには、リストから削除する項目のインデックスを指定します。項目のインデックスではなく、削除する項目そのものを指定するには、Remove メソッドを使用します。リストからすべての項目を削除するには、Clear メソッドを使用します。

使用例

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

ListBox.ObjectCollection クラス
ListBox.ObjectCollection メンバ
System.Windows.Forms 名前空間
Clear
Remove