ListBox.ObjectCollection クラス

定義

ListBox 内の項目のコレクションを表します。

public: ref class ListBox::ObjectCollection : System::Collections::IList
[System.ComponentModel.ListBindable(false)]
public class ListBox.ObjectCollection : System.Collections.IList
[<System.ComponentModel.ListBindable(false)>]
type ListBox.ObjectCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public Class ListBox.ObjectCollection
Implements IList
継承
ListBox.ObjectCollection
派生
属性
実装

次のコード例では、 プロパティを 値に設定し、 イベントと MeasureItem イベントをOwnerDrawVariableDrawMode処理することで、所有者が描画ListBoxする方法をDrawItem示します。 また、 プロパティと ScrollAlwaysVisible プロパティをBorderStyle設定し、 メソッドを使用する方法についても説明しますAddRange

この例を実行するには、名前空間と名前空間をインポートする空のフォームにSystem.DrawingSystem.Windows.Forms貼り付けます。 フォームのコンストラクターまたはLoadメソッドから を呼び出しますInitializeOwnerDrawnListBox

internal:
   System::Windows::Forms::ListBox^ ListBox1;

private:
   void InitializeOwnerDrawnListBox()
   {
      this->ListBox1 = gcnew System::Windows::Forms::ListBox;
      
      // Set the location and size.
      ListBox1->Location = Point(20,20);
      ListBox1->Size = System::Drawing::Size( 240, 240 );
      
      // Populate the ListBox.ObjectCollection property 
      // with several strings, using the AddRange method.
      array<Object^>^temp0 = {"System.Windows.Forms","System.Drawing","System.Xml","System.Net","System.Runtime.Remoting","System.Web"};
      this->ListBox1->Items->AddRange( temp0 );
      
      // Turn off the scrollbar.
      ListBox1->ScrollAlwaysVisible = false;
      
      // Set the border style to a single, flat border.
      ListBox1->BorderStyle = BorderStyle::FixedSingle;
      
      // Set the DrawMode property to the OwnerDrawVariable value. 
      // This means the MeasureItem and DrawItem events must be 
      // handled.
      ListBox1->DrawMode = DrawMode::OwnerDrawVariable;
      ListBox1->MeasureItem += gcnew MeasureItemEventHandler( this, &Form1::ListBox1_MeasureItem );
      ListBox1->DrawItem += gcnew DrawItemEventHandler( this, &Form1::ListBox1_DrawItem );
      this->Controls->Add( this->ListBox1 );
   }

   // Handle the DrawItem event for an owner-drawn ListBox.
   void ListBox1_DrawItem( Object^ /*sender*/, DrawItemEventArgs^ e )
   {
      // If the item is the selected item, then draw the rectangle
      // filled in blue. The item is selected when a bitwise And  
      // of the State property and the DrawItemState.Selected 
      // property is true.
      if ( (e->State & DrawItemState::Selected) == DrawItemState::Selected )
      {
         e->Graphics->FillRectangle( Brushes::CornflowerBlue, e->Bounds );
      }
      else
      {
         
         // Otherwise, draw the rectangle filled in beige.
         e->Graphics->FillRectangle( Brushes::Beige, e->Bounds );
      }
      
      // Draw a rectangle in blue around each item.
      e->Graphics->DrawRectangle( Pens::Blue, e->Bounds );
      
      // Draw the text in the item.
      e->Graphics->DrawString( ListBox1->Items[ e->Index ]->ToString(), this->Font, Brushes::Black, (float)e->Bounds.X, (float)e->Bounds.Y );
      
      // Draw the focus rectangle around the selected item.
      e->DrawFocusRectangle();
   }


   // Handle the MeasureItem event for an owner-drawn ListBox.
   void ListBox1_MeasureItem( Object^ sender, MeasureItemEventArgs^ e )
   {
      
      // Cast the sender object back to ListBox type.
      ListBox^ theListBox = dynamic_cast<ListBox^>(sender);
      
      // Get the string contained in each item.
      String^ itemString = dynamic_cast<String^>(theListBox->Items[ e->Index ]);
      
      // Split the string at the " . "  character.
      array<Char>^temp1 = {'.'};
      array<String^>^resultStrings = itemString->Split( temp1 );
      
      // If the string contains more than one period, increase the 
      // height by ten pixels; otherwise, increase the height by 
      // five pixels.
      if ( resultStrings->Length > 2 )
      {
         e->ItemHeight += 10;
      }
      else
      {
         e->ItemHeight += 5;
      }
   }
internal System.Windows.Forms.ListBox ListBox1;

private void InitializeOwnerDrawnListBox()
{
    this.ListBox1 = new System.Windows.Forms.ListBox();

    // Set the location and size.
    ListBox1.Location = new Point(20, 20);
    ListBox1.Size = new Size(240, 240);

    // Populate the ListBox.ObjectCollection property 
    // with several strings, using the AddRange method.
    this.ListBox1.Items.AddRange(new object[]{"System.Windows.Forms", 
        "System.Drawing", "System.Xml", "System.Net", "System.Runtime.Remoting", 
        "System.Web"});

    // Turn off the scrollbar.
    ListBox1.ScrollAlwaysVisible = false;

    // Set the border style to a single, flat border.
    ListBox1.BorderStyle = BorderStyle.FixedSingle;

    // Set the DrawMode property to the OwnerDrawVariable value. 
    // This means the MeasureItem and DrawItem events must be 
    // handled.
    ListBox1.DrawMode = DrawMode.OwnerDrawVariable;
    ListBox1.MeasureItem += 
        new MeasureItemEventHandler(ListBox1_MeasureItem);
    ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
    this.Controls.Add(this.ListBox1);
}

// Handle the DrawItem event for an owner-drawn ListBox.
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{

    // If the item is the selected item, then draw the rectangle
    // filled in blue. The item is selected when a bitwise And  
    // of the State property and the DrawItemState.Selected 
    // property is true.
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
    {
        e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds);
    }
    else
    {
        // Otherwise, draw the rectangle filled in beige.
        e.Graphics.FillRectangle(Brushes.Beige, e.Bounds);
    }

    // Draw a rectangle in blue around each item.
    e.Graphics.DrawRectangle(Pens.Blue, e.Bounds);

    // Draw the text in the item.
    e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
        this.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y);

    // Draw the focus rectangle around the selected item.
    e.DrawFocusRectangle();
}

// Handle the MeasureItem event for an owner-drawn ListBox.
private void ListBox1_MeasureItem(object sender, 
    MeasureItemEventArgs e)
{

    // Cast the sender object back to ListBox type.
    ListBox theListBox = (ListBox) sender;

    // Get the string contained in each item.
    string itemString = (string) theListBox.Items[e.Index];

    // Split the string at the " . "  character.
    string[] resultStrings = itemString.Split('.');

    // If the string contains more than one period, increase the 
    // height by ten pixels; otherwise, increase the height by 
    // five pixels.
    if (resultStrings.Length>2)
    {
        e.ItemHeight += 10;
    }
    else
    {
        e.ItemHeight += 5;
    }
}
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

Private Sub InitializeOwnerDrawnListBox()
    Me.ListBox1 = New System.Windows.Forms.ListBox

    ' Set the location and size.
    ListBox1.Location = New Point(20, 20)
    ListBox1.Size = New Size(240, 240)

    ' Populate the ListBox.ObjectCollection property 
    ' with several strings, using the AddRange method.
    Me.ListBox1.Items.AddRange(New Object() _
        {"System.Windows.Forms", "System.Drawing", "System.Xml", _
        "System.Net", "System.Runtime.Remoting", "System.Web"})

    ' Turn off the scrollbar.
    ListBox1.ScrollAlwaysVisible = False

    ' Set the border style to a single, flat border.
    ListBox1.BorderStyle = BorderStyle.FixedSingle

    ' Set the DrawMode property to the OwnerDrawVariable value. 
    ' This means the MeasureItem and DrawItem events must be 
    ' handled.
    ListBox1.DrawMode = DrawMode.OwnerDrawVariable
    Me.Controls.Add(Me.ListBox1)
End Sub


' Handle the DrawItem event for an owner-drawn ListBox.
Private Sub ListBox1_DrawItem(ByVal sender As Object, _
    ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem

    ' If the item is the selected item, then draw the rectangle filled in
    ' blue. The item is selected when a bitwise And of the State property
    ' and the DrawItemState.Selected property is true. 
    If (e.State And DrawItemState.Selected = DrawItemState.Selected) Then
        e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds)
    Else
        ' Otherwise, draw the rectangle filled in beige.
        e.Graphics.FillRectangle(Brushes.Beige, e.Bounds)
    End If

    ' Draw a rectangle in blue around each item.
    e.Graphics.DrawRectangle(Pens.Blue, e.Bounds)

    ' Draw the text in the item.
    e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.Font, _
        Brushes.Black, e.Bounds.X, e.Bounds.Y)

    ' Draw the focus rectangle around the selected item.
    e.DrawFocusRectangle()
End Sub

' Handle the MeasureItem event for an owner-drawn ListBox.
Private Sub ListBox1_MeasureItem(ByVal sender As Object, _
    ByVal e As MeasureItemEventArgs) Handles ListBox1.MeasureItem

    ' Cast the sender object back to ListBox type.
    Dim theListBox As ListBox = CType(sender, ListBox)

    ' Get the string contained in each item.
    Dim itemString As String = CType(theListBox.Items(e.Index), String)

    ' Split the string at the " . "  character.
    Dim resultStrings() As String = itemString.Split(".")

    ' If the string contains more than one period, increase the 
    ' height by ten pixels; otherwise, increase the height by 
    ' five pixels.
    If (resultStrings.Length > 2) Then
        e.ItemHeight += 10
    Else
        e.ItemHeight += 5
    End If

End Sub

注釈

クラスは ListBox.ObjectCollection 、 に表示される項目を格納します ListBox。 クラス内には、このコレクション内で選択されている項目を ListBox 決定できる他の 2 つのコレクションが定義されています。 クラスには ListBox.SelectedObjectCollection 、 内で ListBox.ObjectCollection選択されている項目を決定するためのプロパティとメソッドが用意されていますが ListBox.SelectedIndexCollection 、 クラスを使用すると、 内で選択されるインデックスを ListBox.ObjectCollection 決定できます。

コレクションに項目を追加するには、いくつかの方法があります。 メソッドは Add 、コレクションに 1 つのオブジェクトを追加する機能を提供します。 コレクションに多数のオブジェクトを追加するには、項目の配列を作成し、 メソッドに AddRange 割り当てます。 コレクション内の特定の場所にオブジェクトを挿入する場合は、 メソッドを Insert 使用できます。 アイテムを削除するには、コレクション内の Remove 項目の RemoveAt 場所がわかっている場合は、 メソッドまたは メソッドを使用できます。 Clearメソッドを使用すると、 メソッドを使用してRemove一度に 1 つの項目を削除する代わりに、コレクションからすべての項目を削除できます。

プロパティを使用して、 の項目をListBoxDataSource操作することもできます。 プロパティを DataSource 使用して に項目を追加する ListBox場合は、 プロパティを使用して 内の ListBox 項目を Items 表示できますが、 のメソッド ListBox.ObjectCollectionを使用してリストに項目を追加または削除することはできません。

には、項目を追加および削除するためのメソッドとプロパティに加えて、 ListBox.ObjectCollection コレクション内の項目を検索するメソッドも用意されています。 Containsメソッドを使用すると、オブジェクトがコレクションのメンバーであるかどうかを判断できます。 アイテムがコレクション内にあることがわかっている場合は、 メソッドを IndexOf 使用して、アイテムがコレクション内のどこにあるかを判断できます。

コンストラクター

ListBox.ObjectCollection(ListBox)

ListBox.ObjectCollection の新しいインスタンスを初期化します。

ListBox.ObjectCollection(ListBox, ListBox+ObjectCollection)

別の ListBox.ObjectCollection に基づいて、ListBox.ObjectCollection の新しいインスタンスを初期化します。

ListBox.ObjectCollection(ListBox, Object[])

オブジェクトの配列を格納している ListBox.ObjectCollection の新しいインスタンスを初期化します。

プロパティ

Count

コレクション内の項目の数を取得します。

IsReadOnly

コレクションが読み取り専用かどうかを示す値を取得します。

Item[Int32]

コレクション内の指定したインデックスにある項目を取得または設定します。

メソッド

Add(Object)

ListBox の項目のリストに項目を追加します。

AddRange(ListBox+ObjectCollection)

既存の ListBox.ObjectCollection の項目を ListBox の項目のリストに追加します。

AddRange(Object[])

ListBox の項目のリストに、項目の配列を追加します。

Clear()

コレクションからすべての項目を削除します。

Contains(Object)

指定した項目がコレクション内にあるかどうかを確認します。

CopyTo(Object[], Int32)

既存のオブジェクト配列内の指定した位置にコレクション全体をコピーします。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

項目コレクションを反復処理するために使用する列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IndexOf(Object)

指定した項目のコレクション内のインデックスを返します。

Insert(Int32, Object)

リスト ボックス内の指定したインデックスに項目を挿入します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(Object)

指定したオブジェクトをコレクションから削除します。

RemoveAt(Int32)

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

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

ICollection.CopyTo(Array, Int32)

この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。

特定の配列インデックスを開始位置として、配列にコレクションの要素をコピーします。

ICollection.IsSynchronized

このメンバーの詳細については、「IsSynchronized」をご覧ください。

ICollection.SyncRoot

このメンバーの詳細については、「SyncRoot」をご覧ください。

IList.Add(Object)

この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。

オブジェクトを ListBox クラスに追加します。

IList.Contains(Object)

IList に特定の値が格納されているかどうかを判断します。

IList.IndexOf(Object)

IList 内の特定の項目のインデックスを確認します。

IList.Insert(Int32, Object)

指定したインデックスの IList に項目を挿入します。

IList.IsFixedSize

このメンバーの詳細については、「IsFixedSize」をご覧ください。

IList.Item[Int32]

指定したインデックスにある要素を取得または設定します。

IList.Remove(Object)

特定のオブジェクトが IList 内にあるときに、最初に出現したものを削除します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

こちらもご覧ください