Freigeben über


ListBox.DrawItem-Ereignis

Tritt ein, wenn sich ein Darstellungsaspekt einer Ownerdrawn-ListBox ändert.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Event DrawItem As DrawItemEventHandler
'Usage
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 unterstützt die Verwendung von Ereignissen, aber nicht die Deklaration von neuen Ereignissen.

Hinweise

Dieses Ereignis wird von einer Ownerdrawn-ListBox verwendet. Das Ereignis wird nur ausgelöst, wenn die DrawMode-Eigenschaft auf DrawMode.OwnerDrawFixed oder DrawMode.OwnerDrawVariable festgelegt ist. Mithilfe dieses Ereignisses können Sie die Aufgaben durchführen, die zum Zeichnen der Elemente in der ListBox erforderlich sind. Bei einem Element mit variabler Größe (die DrawMode-Eigenschaft ist auf DrawMode.OwnerDrawVariable festgelegt) wird vor dem Zeichnen eines Elements das MeasureItem-Ereignis ausgelöst. Sie können einen Ereignishandler für das MeasureItem-Ereignis erstellen, um die Größe für das Element anzugeben, das im Ereignishandler für das DrawItem-Ereignis gezeichnet werden soll.

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter Behandeln von Ereignissen.

Beispiel

Im folgenden Codebeispiel wird das Erstellen von Ownerdrawn-ListBox-Elementen veranschaulicht. Mit der DrawMode-Eigenschaft gibt der Code an, dass die gezeichneten Elemente eine feste Größe besitzen, und durch das DrawItem-Ereignis zeichnet der Code jedes Element in der ListBox. Der Beispielcode zeichnet die Elemente mithilfe der Eigenschaften und Methoden der DrawItemEventArgs-Klasse, die als Parameter an den Ereignishandler übergeben wurde. Für dieses Beispiel muss einem Formular das ListBox-Steuerelement listBox1 hinzugefügt werden, und das DrawItem-Ereignis muss von einem im Beispielcode definierten Ereignishandler behandelt werden. Für das Beispiel müssen außerdem der ListBox Elemente hinzugefügt werden, wobei der Text "Apple", "Orange" und "Plum" in der angegebenen Reihenfolge lautet.

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

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

ListBox-Klasse
ListBox-Member
System.Windows.Forms-Namespace
OnDrawItem