Export (0) Print
Expand All

ListView.DrawSubItem Event

Occurs when the details view of a ListView is drawn and the OwnerDraw property is set to true.

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

public event DrawListViewSubItemEventHandler DrawSubItem
/** @event */
public void add_DrawSubItem (DrawListViewSubItemEventHandler value)

/** @event */
public void remove_DrawSubItem (DrawListViewSubItemEventHandler value)

In JScript, you can handle the events defined by a class, but you cannot define your own.
Not applicable.

This event lets you customize the appearance of a ListView control using owner drawing. It is raised only when the OwnerDraw property is set to true and the View property is set to View.Details. For more information on owner drawing, see the OwnerDraw property reference topic.

NoteNote:

Subitem information typically appears in the tile view as well as the details view, but in the tile view, it must be drawn in a handler for the DrawItem event.

The DrawSubItem event can occur for each ListView subitem. You can handle the DrawItem event to draw elements common to all subitems, such as the background, and handle the DrawSubItem event to draw elements for individual subitems, such as text values. You can also draw all items in the ListView control using only one of the two events, although this may be less convenient. To draw column headers in the details view, you must handle the DrawColumnHeader event.

NoteNote:

The DrawSubItem event does not occur for any subitems for which no ColumnHeader object has been added to the Columns collection. Note also that the first subitem of each ListViewItem object represents the parent item itself, and is displayed in the first column.

For more information about handling events, see Consuming Events.

The following code example provides an implementation of a DrawSubItem event handler. For the complete example, see the OwnerDraw reference topic.

// Draws subitem text and applies content-based formatting.
private void listView1_DrawSubItem(object sender,
    DrawListViewSubItemEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Left;

    using (StringFormat sf = new StringFormat())
    {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        switch (e.Header.TextAlign)
        {
            case HorizontalAlignment.Center:
                sf.Alignment = StringAlignment.Center;
                flags = TextFormatFlags.HorizontalCenter;
                break;
            case HorizontalAlignment.Right:
                sf.Alignment = StringAlignment.Far;
                flags = TextFormatFlags.Right;
                break;
        }

        // Draw the text and background for a subitem with a 
        // negative value. 
        double subItemValue;
        if (e.ColumnIndex > 0 && Double.TryParse(
            e.SubItem.Text, NumberStyles.Currency,
            NumberFormatInfo.CurrentInfo, out subItemValue) &&
            subItemValue < 0)
        {
            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if ((e.ItemState & ListViewItemStates.Selected) == 0)
            {
                e.DrawBackground();
            }

            // Draw the subitem text in red to highlight it. 
            e.Graphics.DrawString(e.SubItem.Text,
                listView1.Font, Brushes.Red, e.Bounds, sf);

            return;
        }

        // Draw normal text for a subitem with a nonnegative 
        // or nonnumerical value.
        e.DrawText(flags);
    }
}

// Draws subitem text and applies content-based formatting.
private void myListView_DrawSubItem(Object sender,
    DrawListViewSubItemEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Left;
    StringFormat sf = new StringFormat();
    try {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        if (e.get_Header().get_TextAlign().
            Equals(HorizontalAlignment.Center)) {

            sf.set_Alignment(StringAlignment.Center);
            flags = TextFormatFlags.HorizontalCenter;
        }
        else {
            if (e.get_Header().get_TextAlign().
                Equals(HorizontalAlignment.Right)) {

                sf.set_Alignment(StringAlignment.Far);
                flags = TextFormatFlags.Right;
            }
        }
        // Draw the text for a column header.
        if (e.get_ItemIndex() == -1) {
            Font myFont = new Font("Helvetica", 12, FontStyle.Bold);
            try {
                e.get_Graphics().DrawString(e.get_Item().get_Text(), 
                    myFont, Brushes.get_White(), 
                    new PointF((float)e.get_Bounds().get_X(), 
                    (float)e.get_Bounds().get_Y()), sf);
            }
            finally {
                myFont.Dispose();
            }
            return;
        }

        // Draw the text and background for a subitem with a 
        // negative value. 
        double subItemValue = 0;
        if (e.get_ColumnIndex() > 0 && System.Double.TryParse(
            e.get_Item().get_SubItems().get_Item(e.get_ColumnIndex()).
            get_Text(), NumberStyles.Currency, 
            NumberFormatInfo.get_CurrentInfo(), subItemValue) 
            && subItemValue < 0) {

            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if (Convert.ToInt32(e.get_ItemState() 
                & ListViewItemStates.Selected) == 0) {
                e.DrawBackground();
            }
            // Draw the subitem text in red to highlight it. 
            e.get_Graphics().DrawString(e.get_Item().get_SubItems().
                get_Item(e.get_ColumnIndex()).get_Text(), 
                ((ListView)sender).get_Font(), Brushes.get_Red(), 
                RectangleF.op_Implicit(e.get_Bounds()), sf);

            return;
        }
        // Draw normal text for a subitem with a nonnegative 
        // or nonnumerical value.
        e.DrawText(flags);
    }
    finally {
        sf.Dispose();
    }
} //myListView_DrawSubItem

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft