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
衍生
屬性
實作

範例

下列程式碼範例示範如何為主控描繪ListBoxsplittunnelingDrawMode屬性設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類別會儲存在顯示的項目ListBoxThe ListBox.ObjectCollection class stores the items displayed in the ListBox. 有兩個定義內的其他集合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方法讓您能夠加入至集合的單一物件。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方法以移除單一項目一次。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.ObjectCollectionIf 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()

取得目前執行個體的 TypeGets 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 的淺層複本 (Shallow Copy)。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

如需這個成員的說明,請參閱 IsSynchronizedFor a description of this member, see IsSynchronized.

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

如需這個成員的說明,請參閱 SyncRootFor 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

如需這個成員的說明,請參閱 IsFixedSizeFor a description of this member, see IsFixedSize.

Extension Methods

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)

將轉換IEnumerableIQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱