다음을 통해 공유


ListBox.DrawItem 이벤트

소유자가 그린 ListBox의 시각적 측면이 변경될 때 발생합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Event DrawItem As DrawItemEventHandler
‘사용 방법
Dim instance As ListBox
Dim handler As DrawItemEventHandler

AddHandler instance.DrawItem, handler
public event DrawItemEventHandler DrawItem
public:
event DrawItemEventHandler^ DrawItem {
    void add (DrawItemEventHandler^ value);
    void remove (DrawItemEventHandler^ value);
}
/** @event */
public void add_DrawItem (DrawItemEventHandler value)

/** @event */
public void remove_DrawItem (DrawItemEventHandler value)
JScript에서는 이벤트를 사용할 수 있지만 새로 선언할 수는 없습니다.

설명

이 이벤트는 소유자가 그린 ListBox에서 사용됩니다. 이 이벤트는 DrawMode 속성이 DrawMode.OwnerDrawFixed 또는 DrawMode.OwnerDrawVariable로 설정되어 있을 때에만 발생합니다. 이 이벤트를 사용하여 ListBox의 항목을 그리는 데 필요한 작업을 수행할 수 있습니다. 가변 크기 항목이 있으면(DrawMode 속성이 DrawMode.OwnerDrawVariable로 설정된 경우) 항목을 그리기 전에 MeasureItem 이벤트가 발생합니다. MeasureItem 이벤트에 대한 이벤트 처리기를 만들어 DrawItem 이벤트의 이벤트 처리기에 그리려는 항목의 크기를 지정할 수 있습니다.

이벤트 처리에 대한 자세한 내용은 이벤트 사용을 참조하십시오.

예제

다음 코드 예제에서는 소유자가 그린 ListBox 항목을 만드는 방법을 보여 줍니다. 이 코드는 DrawMode 속성을 통해 그려진 항목의 크기가 고정되도록 지정하며, DrawItem 이벤트를 통해 각 항목을 ListBox 내에 그려넣습니다. 다음 예제 코드에서는 이벤트 처리기에 매개 변수로 전달된 DrawItemEventArgs 클래스의 속성 및 메서드를 사용하여 항목을 그립니다. 이 예제를 실행하려면 listBox1이라는 ListBox 컨트롤이 폼에 추가되어 있고, 이 예제 코드에 정의된 이벤트 처리기에서 DrawItem 이벤트를 처리해야 합니다. 또한 이 예제를 실행하려면 "Apple", "Orange" 및 "Plum"이라는 텍스트를 가진 항목이 순서대로 ListBox에 추가되어야 합니다.

Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
   ' Set the DrawMode property to draw fixed sized items.
   ListBox1.DrawMode = DrawMode.OwnerDrawFixed
   ' Draw the background of the ListBox control for each item.
   e.DrawBackground()
   ' Define the default color of the brush as black.
   Dim myBrush As Brush

   ' Determine the color of the brush to draw each item based on the index of the item to draw.
   Select Case (e.Index)
      Case 0
         myBrush = Brushes.Red
      Case 1
         myBrush = Brushes.Orange
      Case 2
         myBrush = Brushes.Purple
   End Select

   ' Draw the current item text based on the current Font and the custom brush settings.
   e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
   ' If the ListBox has focus, draw a focus rectangle around the selected item.
   e.DrawFocusRectangle()
End Sub
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
   // Set the DrawMode property to draw fixed sized items.
   listBox1.DrawMode = DrawMode.OwnerDrawFixed;
   // Draw the background of the ListBox control for each item.
   e.DrawBackground();
   // Define the default color of the brush as black.
   Brush myBrush = Brushes.Black;

   // Determine the color of the brush to draw each item based on the index of the item to draw.
   switch (e.Index)
   {
      case 0:
         myBrush = Brushes.Red;
         break;
      case 1:
         myBrush = Brushes.Orange;
         break;
      case 2:
         myBrush = Brushes.Purple;
         break;
   }

   // Draw the current item text based on the current Font and the custom brush settings.
   e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush,e.Bounds,StringFormat.GenericDefault);
   // If the ListBox has focus, draw a focus rectangle around the selected item.
   e.DrawFocusRectangle();
}
private:
   void listBox1_DrawItem( Object^ /*sender*/, System::Windows::Forms::DrawItemEventArgs^ e )
   {
      // Set the DrawMode property to draw fixed sized items.
      listBox1->DrawMode = DrawMode::OwnerDrawFixed;

      // Draw the background of the ListBox control for each item.
      e->DrawBackground();

      // Define the default color of the brush as black.
      Brush^ myBrush = Brushes::Black;

      // Determine the color of the brush to draw each item based on the index of the item to draw.
      switch ( e->Index )
      {
         case 0:
            myBrush = Brushes::Red;
            break;

         case 1:
            myBrush = Brushes::Orange;
            break;

         case 2:
            myBrush = Brushes::Purple;
            break;
      }

      // Draw the current item text based on the current Font and the custom brush settings.
      e->Graphics->DrawString( listBox1->Items[ e->Index ]->ToString(), e->Font, myBrush, e->Bounds, StringFormat::GenericDefault );

      // If the ListBox has focus, draw a focus rectangle around the selected item.
      e->DrawFocusRectangle();
   }
private void listBox1_DrawItem(Object sender,
    System.Windows.Forms.DrawItemEventArgs e)
{
    // Set the DrawMode property to draw fixed sized items.
    listBox1.set_DrawMode(DrawMode.OwnerDrawFixed);
    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Create a new Brush and initialize to a Black colored brush
    // by default.
    Brush myBrush = Brushes.get_Black();
    // Determine the color of the brush to draw each item based on the
    // index of the item to draw.
    switch (e.get_Index()) {
        case 0 :
            myBrush = Brushes.get_Red();
            break;
        case 1 :
            myBrush = Brushes.get_Orange();
            break;
        case 2 :
            myBrush = Brushes.get_Purple();
            break;
    }

    // Draw the current item text based on the current Font and the custom
    // brush settings.
    e.get_Graphics().DrawString(System.Convert.ToString(listBox1.
        get_Items().get_Item(e.get_Index())), e.get_Font(), myBrush,
        RectangleF.op_Implicit(e.get_Bounds()), StringFormat.
        get_GenericDefault());
    // If the ListBox has focus, draw a focus rectangle around the selected
    // item.
    e.DrawFocusRectangle();
} //listBox1_DrawItem

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

ListBox 클래스
ListBox 멤버
System.Windows.Forms 네임스페이스
OnDrawItem