共用方式為


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

範例

下列程式碼範例示範將 屬性設定 DrawMode 為 值及處理 DrawItemMeasureItem 事件的擁有者 ListBoxOwnerDrawVariable 。 它也會示範如何設定 BorderStyleScrollAlwaysVisible 屬性,以及使用 AddRange 方法。

若要執行此範例,請將它貼到匯入命名空間和命名空間的 System.Windows.Forms 空白表單 System.Drawing 中。 從表單的建構函式或 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 定義了兩個其他集合,可讓您判斷在此集合中選取的專案。 類別 ListBox.SelectedObjectCollection 提供屬性和方法,用來判斷 在 內 ListBox.ObjectCollection 選取的專案,而 ListBox.SelectedIndexCollection 類別可讓您判斷 選取內的 ListBox.ObjectCollection 索引。

有數種方式可將專案新增至集合。 方法 Add 可讓您將單一物件加入至集合。 若要將一些物件加入集合中,您可以建立專案陣列,並將它指派給 AddRange 方法。 如果您想要在集合內的特定位置插入 物件,您可以使用 Insert 方法。 若要移除專案,如果您知道專案位於集合中的位置,則可以使用 Remove 方法或 RemoveAt 方法。 方法 Clear 可讓您從集合中移除所有專案,而不是使用 Remove 方法一次移除單一專案。

您也可以使用 DataSource 屬性來操作 的專案 ListBox 。 如果您使用 DataSource 屬性將專案新增至 ListBox ,您可以使用 屬性檢視 中的 ListBoxItems 專案,但無法使用 的 方法 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)

IEnumerable 轉換成 IQueryable

適用於

另請參閱