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. 설정 하는 방법도 보여 줍니다 합니다 BorderStyle 하 고 ScrollAlwaysVisible 속성 및 사용 하 여는 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. 내에 정의 된 다른 두 컬렉션을 가지는 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.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.

적용 대상

추가 정보