ListView.DrawSubItem Event

Definition

当绘制 ListView 的详细信息视图并且 OwnerDraw 属性设置为 true 时发生。Occurs when the details view of a ListView is drawn and the OwnerDraw property is set to true.

public:
 event System::Windows::Forms::DrawListViewSubItemEventHandler ^ DrawSubItem;
public event System.Windows.Forms.DrawListViewSubItemEventHandler DrawSubItem;
member this.DrawSubItem : System.Windows.Forms.DrawListViewSubItemEventHandler 
Public Custom Event DrawSubItem As DrawListViewSubItemEventHandler 

Examples

下面的代码示例提供了 DrawSubItem 事件处理程序的实现。The following code example provides an implementation of a DrawSubItem event handler. 有关完整示例,请参阅 "OwnerDraw 参考" 主题。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 Sub listView1_DrawSubItem(ByVal sender As Object, _
    ByVal e As DrawListViewSubItemEventArgs) _
    Handles listView1.DrawSubItem

    Dim flags As TextFormatFlags = TextFormatFlags.Left

    Dim sf As New StringFormat()
    Try

        ' Store the column text alignment, letting it default
        ' to Left if it has not been set to Center or Right.
        Select Case e.Header.TextAlign
            Case HorizontalAlignment.Center
                sf.Alignment = StringAlignment.Center
                flags = TextFormatFlags.HorizontalCenter
            Case HorizontalAlignment.Right
                sf.Alignment = StringAlignment.Far
                flags = TextFormatFlags.Right
        End Select

        ' Draw the text and background for a subitem with a 
        ' negative value. 
        Dim subItemValue As Double
        If e.ColumnIndex > 0 AndAlso _
            Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
            NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
            subItemValue < 0 Then

            ' Unless the item is selected, draw the standard 
            ' background to make it stand out from the gradient.
            If (e.ItemState And ListViewItemStates.Selected) = 0 Then
                e.DrawBackground()
            End If

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

            Return

        End If

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

    Finally
        sf.Dispose()
    End Try

End Sub

Remarks

此事件使你可以使用所有者描述自定义 ListView 控件的外观。This event lets you customize the appearance of a ListView control using owner drawing. 仅当 OwnerDraw 属性设置为 true 并且 View 属性设置为 View.Details时才引发。It is raised only when the OwnerDraw property is set to true and the View property is set to View.Details. 有关所有者描述的详细信息,请参阅 OwnerDraw 属性参考主题。For more information on owner drawing, see the OwnerDraw property reference topic.

Note

子项信息通常显示在图块视图和详细信息视图中,但在磁贴视图中,必须在 DrawItem 事件的处理程序中进行绘制。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.

对于每个 ListView 子项,都可以执行 DrawSubItem 事件。The DrawSubItem event can occur for each ListView subitem. 您可以处理 DrawItem 事件以绘制所有子项(如背景)所共有的元素,并处理 DrawSubItem 事件以绘制单个子项(如文本值)的元素。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. 你还可以仅使用两个事件中的一个来绘制 ListView 控件中的所有项,但这可能不太方便。You can also draw all items in the ListView control using only one of the two events, although this may be less convenient. 若要在详细信息视图中绘制列标题,必须处理 DrawColumnHeader 事件。To draw column headers in the details view, you must handle the DrawColumnHeader event.

Note

对于没有 ColumnHeader 对象添加到 Columns 集合的任何子项,不会发生 DrawSubItem 事件。The DrawSubItem event does not occur for any subitems for which no ColumnHeader object has been added to the Columns collection. 另请注意,每个 ListViewItem 对象的第一个子项表示父项本身,并显示在第一列中。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 Handling and Raising Events.

Applies to

See also