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
파생
특성
구현

예제

다음 코드 예제에서는 값을 속성을 OwnerDrawVariable 설정 DrawMode 하 고 처리 하 여 소유자 그리기 ListBoxMeasureItem 이벤트를 보여 DrawItem 줍니다. 또한 및 ScrollAlwaysVisible 속성을 설정하고 BorderStyle 메서드를 사용하는 방법을 AddRange 보여 줍니다.

이 예제를 실행하려면 네임스페이스와 네임스페이스를 System.Drawing 가져오는 빈 양식에 System.Windows.Forms 붙여넣습니다. 호출 InitializeOwnerDrawnListBox 폼의 생성자에서 또는 Load 메서드.

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.ObjectCollectionListBox.SelectedIndexCollection 제공하지만, 클래스를 사용하면 내에서 ListBox.ObjectCollection 선택된 인덱스를 확인할 수 있습니다.

컬렉션에 항목을 추가하는 방법에는 여러 가지가 있습니다. 메서드는 Add 컬렉션에 단일 개체를 추가하는 기능을 제공합니다. 컬렉션에 개체 수를 추가하려면 항목 배열을 만들고 메서드에 AddRange 할당합니다. 컬렉션 내의 특정 위치에 개체를 삽입하려는 경우 메서드를 Insert 사용할 수 있습니다. 항목을 제거하려면 컬렉션 내에서 항목이 Remove 있는 위치를 알고 있는 경우 메서드 또는 RemoveAt 메서드를 사용할 수 있습니다. 메서드 Clear 를 사용하면 메서드를 사용하여 Remove 한 번에 단일 항목을 제거하는 대신 컬렉션에서 모든 항목을 제거할 수 있습니다.

속성을 사용하여 DataSourceListBox 항목을 조작할 수도 있습니다. 사용 하는 경우는 DataSource 속성을 사용 하 여에서 항목을 볼 수 있습니다는 에 항목을 ListBoxListBox 추가 하는 속성을 사용 하 여 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로 변환합니다.

적용 대상

추가 정보