ListBox.ObjectCollection ListBox.ObjectCollection ListBox.ObjectCollection ListBox.ObjectCollection Class

定義

ListBox 内の項目のコレクションを表します。Represents the collection of items in a ListBox.

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

次のコード例に示しますオーナー描画ListBoxを設定して、DrawModeプロパティをOwnerDrawVariable値と処理、DrawItemMeasureItemイベント。The following code example demonstrates an owner-drawn ListBox by setting the DrawMode property to the OwnerDrawVariable value and handling the DrawItem and MeasureItem events. 設定も示しています、BorderStyleScrollAlwaysVisibleプロパティとを使用して、AddRangeメソッド。It also demonstrates setting the BorderStyle and ScrollAlwaysVisible properties and using the AddRange method.

この例を実行するをインポートする空のフォームに貼り付け、System.Drawing名前空間とSystem.Windows.Forms名前空間。To run this example, paste it into an empty form that imports the System.Drawing namespace and the System.Windows.Forms namespace. 呼び出すInitializeOwnerDrawnListBoxフォームのコンス トラクターからまたはLoadメソッド。Call InitializeOwnerDrawnListBox from the form's constructor or Load method.

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します。The ListBox.ObjectCollection class stores the items displayed in the ListBox. 内で定義されたその他の 2 つのコレクションがある、ListBoxこのコレクション内でどのような項目が選択されているかを判断するためのクラス。There are two other collections defined within the ListBox class that enable you to determine what items are selected within this collection. ListBox.SelectedObjectCollectionクラス プロパティとメソッド内でどのような項目が選択されているを決定するための提供、 ListBox.ObjectCollection、中に、ListBox.SelectedIndexCollectionクラスでは、内でインデックスを決定することができます、ListBox.ObjectCollectionが選択されています。The ListBox.SelectedObjectCollection class provides properties and methods for determining what items are selected within the ListBox.ObjectCollection, while the ListBox.SelectedIndexCollection class enables you to determine what indexes within the ListBox.ObjectCollection are selected.

コレクションに項目を追加する方法を数多くあります。There are a number of ways to add items to the collection. Addメソッドが 1 つのオブジェクトをコレクションに追加する機能を提供します。The Add method provides the ability to add a single object to the collection. コレクションにオブジェクトの数を追加する項目の配列を作成およびに割り当てる、AddRangeメソッド。To add a number of objects to the collection, you create an array of items and assign it to the AddRange method. コレクション内の特定の場所にオブジェクトを挿入する場合は、使用、Insertメソッド。If you want to insert an object at a specific location within the collection, you can use the Insert method. 項目を削除する、いずれかを使用できる、RemoveメソッドまたはRemoveAtメソッド項目がコレクション内にあることがわかっている場合。To remove items, you can use either the Remove method or the RemoveAt method if you know where the item is located within the collection. Clearメソッドでは、使用する代わりに、コレクションからすべての項目を削除することができます、Remove一度に 1 つの項目を削除する方法。The Clear method enables you to remove all items from the collection instead of using the Remove method to remove a single item at a time.

項目を操作することも、ListBoxを使用して、DataSourceプロパティ。You can also manipulate the items of a ListBox by using the DataSource property. 使用する場合、DataSourceプロパティに項目を追加、 ListBox、内の項目を表示することができます、ListBoxを使用して、Itemsプロパティが追加または削除できない項目のメソッドを使用してリストから、ListBox.ObjectCollectionします。If you use the DataSource property to add items to a ListBox, you can view the items in the ListBox using the Items property but you cannot add or remove items from the list using the methods of the ListBox.ObjectCollection.

メソッドとプロパティを追加すると、項目を削除するだけでなく、ListBox.ObjectCollectionコレクション内の項目を検索するメソッドも提供されます。In addition to methods and properties for adding and removing items, the ListBox.ObjectCollection also provides methods to find items within the collection. Containsメソッドでは、オブジェクト、コレクションのメンバーであるかどうかを判断することができます。The Contains method enables you to determine whether an object is a member of the collection. 使用することができます、項目がコレクション内にあることがわかったら、IndexOf項目がコレクション内にあるかを判断するメソッド。Once you know that the item is located within the collection, you can use the IndexOf method to determine where the item is located within the collection.

コンストラクター

ListBox.ObjectCollection(ListBox) ListBox.ObjectCollection(ListBox) ListBox.ObjectCollection(ListBox) ListBox.ObjectCollection(ListBox)

ListBox.ObjectCollection の新しいインスタンスを初期化します。Initializes a new instance of ListBox.ObjectCollection.

ListBox.ObjectCollection(ListBox, ListBox+ObjectCollection) ListBox.ObjectCollection(ListBox, ListBox+ObjectCollection) ListBox.ObjectCollection(ListBox, ListBox+ObjectCollection) ListBox.ObjectCollection(ListBox, ListBox+ObjectCollection)

別の ListBox.ObjectCollection に基づいて、ListBox.ObjectCollection の新しいインスタンスを初期化します。Initializes a new instance of ListBox.ObjectCollection based on another ListBox.ObjectCollection.

ListBox.ObjectCollection(ListBox, Object[]) ListBox.ObjectCollection(ListBox, Object[]) ListBox.ObjectCollection(ListBox, Object[]) ListBox.ObjectCollection(ListBox, Object[])

オブジェクトの配列を格納している ListBox.ObjectCollection の新しいインスタンスを初期化します。Initializes a new instance of ListBox.ObjectCollection containing an array of objects.

プロパティ

Count Count Count Count

コレクション内の項目の数を取得します。Gets the number of items in the collection.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

コレクションが読み取り専用かどうかを示す値を取得します。Gets a value indicating whether the collection is read-only.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

コレクション内の指定したインデックスにある項目を取得または設定します。Gets or sets the item at the specified index within the collection.

メソッド

Add(Object) Add(Object) Add(Object) Add(Object)

ListBox の項目のリストに項目を追加します。Adds an item to the list of items for a ListBox.

AddRange(ListBox+ObjectCollection) AddRange(ListBox+ObjectCollection) AddRange(ListBox+ObjectCollection) AddRange(ListBox+ObjectCollection)

既存の ListBox.ObjectCollection の項目を ListBox の項目のリストに追加します。Adds the items of an existing ListBox.ObjectCollection to the list of items in a ListBox.

AddRange(Object[]) AddRange(Object[]) AddRange(Object[]) AddRange(Object[])

ListBox の項目のリストに、項目の配列を追加します。Adds an array of items to the list of items for a ListBox.

Clear() Clear() Clear() Clear()

コレクションからすべての項目を削除します。Removes all items from the collection.

Contains(Object) Contains(Object) Contains(Object) Contains(Object)

指定した項目がコレクション内にあるかどうかを確認します。Determines whether the specified item is located within the collection.

CopyTo(Object[], Int32) CopyTo(Object[], Int32) CopyTo(Object[], Int32) CopyTo(Object[], Int32)

既存のオブジェクト配列内の指定した位置にコレクション全体をコピーします。Copies the entire collection into an existing array of objects at a specified location within the array.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

項目コレクションを反復処理するために使用する列挙子を返します。Returns an enumerator to use to iterate through the item collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
IndexOf(Object) IndexOf(Object) IndexOf(Object) IndexOf(Object)

指定した項目のコレクション内のインデックスを返します。Returns the index within the collection of the specified item.

Insert(Int32, Object) Insert(Int32, Object) Insert(Int32, Object) Insert(Int32, Object)

リスト ボックス内の指定したインデックスに項目を挿入します。Inserts an item into the list box at the specified index.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(Object) Remove(Object) Remove(Object) Remove(Object)

指定されたオブジェクトをコレクションから削除します。Removes the specified object from the collection.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

コレクション内の指定されたインデックスにある項目を削除します。Removes the item at the specified index within the collection.

ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

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

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

特定の配列インデックスを開始位置として、配列にコレクションの要素をコピーします。Copies the elements of the collection to an array, starting at a particular array index.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

このメンバーの説明については、IsSynchronized のトピックを参照してください。For a description of this member, see IsSynchronized.

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

このメンバーの説明については、SyncRoot のトピックを参照してください。For a description of this member, see SyncRoot.

IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)

オブジェクトを ListBox クラスに追加します。Adds an object to the ListBox class.

IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize

このメンバーの説明については、IsFixedSize のトピックを参照してください。For a description of this member, see IsFixedSize.

拡張メソッド

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

クエリの並列化を有効にします。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

変換、IEnumerableIQueryableします。Converts an IEnumerable to an IQueryable.

適用対象

こちらもご覧ください