Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

ListBox.DrawItem-Ereignis

 

Veröffentlicht: Oktober 2016

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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event DrawItemEventHandler DrawItem

This event is used by an owner-drawn T:System.Windows.Forms.ListBox. The event is only raised when the P:System.Windows.Forms.ListBox.DrawMode property is set to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable. You can use this event to perform the tasks needed to draw items in the T:System.Windows.Forms.ListBox. If you have a variable-sized item (when the P:System.Windows.Forms.ListBox.DrawMode property is set to DrawMode.OwnerDrawVariable), before drawing an item, the E:System.Windows.Forms.ListBox.MeasureItem event is raised. You can create an event handler for the E:System.Windows.Forms.ListBox.MeasureItem event to specify the size for the item that you are going to draw in your event handler for the E:System.Windows.Forms.ListBox.DrawItem event.

For more information about handling events, see Handling and Raising Events.

The following code example demonstrates how to create owner-drawn T:System.Windows.Forms.ListBox items. The code uses the P:System.Windows.Forms.ListBox.DrawMode property to specify that the items drawn are fixed sized and the E:System.Windows.Forms.ListBox.DrawItem event to perform the drawing of each item into the T:System.Windows.Forms.ListBox. The example code uses the properties and methods of the T:System.Windows.Forms.DrawItemEventArgs class passed as a parameter to the event handler to draw the items. This example requires that a T:System.Windows.Forms.ListBox control called listBox1 has been added to a form and that the E:System.Windows.Forms.ListBox.DrawItem event is handled by the event handler defined in the example code. The example also requires that items have been added to the T:System.Windows.Forms.ListBox with the text of "Apple", "Orange", and "Plum" in that order.

private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
    ListBox1.Items.AddRange(new Object[] 
        { "Red Item", "Orange Item", "Purple Item" });
    ListBox1.Location = new System.Drawing.Point(81, 69);
    ListBox1.Size = new System.Drawing.Size(120, 95);
    ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
    ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
    Controls.Add(ListBox1);
}

private void ListBox1_DrawItem(object sender, 
    System.Windows.Forms.DrawItemEventArgs e)
{
    // 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();
}

.NET Framework
Verfügbar seit 1.1
Zurück zum Anfang
Anzeigen: