Edit

Share via


DrawListViewSubItemEventArgs.DrawText Method

Definition

Draws the text of the ListViewItem.ListViewSubItem using its current foreground color and the default formatting.

Overloads

DrawText()

Draws the text of the ListViewItem.ListViewSubItem using its current foreground color.

DrawText(TextFormatFlags)

Draws the text of the ListViewItem.ListViewSubItem using its current foreground color and formatting it with the specified TextFormatFlags values.

DrawText()

Source:
DrawListViewSubItemEventArgs.cs
Source:
DrawListViewSubItemEventArgs.cs
Source:
DrawListViewSubItemEventArgs.cs

Draws the text of the ListViewItem.ListViewSubItem using its current foreground color.

public:
 void DrawText();
public void DrawText ();
member this.DrawText : unit -> unit
Public Sub DrawText ()

Remarks

Use this method to draw the subitem text using the current values of the ListViewItem.ListViewSubItem.ForeColor and ListViewItem.ListViewSubItem.Font properties. The text is drawn within the area specified by the Bounds property.

Note

The UseItemStyleForSubItems property of the parent ListViewItem must be set to false to prevent the ListViewItem.ForeColor and ListViewItem.Font values of the parent item from overriding the subitem values.

See also

Applies to

DrawText(TextFormatFlags)

Source:
DrawListViewSubItemEventArgs.cs
Source:
DrawListViewSubItemEventArgs.cs
Source:
DrawListViewSubItemEventArgs.cs

Draws the text of the ListViewItem.ListViewSubItem using its current foreground color and formatting it with the specified TextFormatFlags values.

public:
 void DrawText(System::Windows::Forms::TextFormatFlags flags);
public void DrawText (System.Windows.Forms.TextFormatFlags flags);
member this.DrawText : System.Windows.Forms.TextFormatFlags -> unit
Public Sub DrawText (flags As TextFormatFlags)

Parameters

flags
TextFormatFlags

A bitwise combination of TextFormatFlags values.

Examples

The following code example demonstrates how to use the DrawText method in an application that provides custom drawing for a ListView control. In the example, a handler for the ListView.DrawSubItem event draws the subitem text values and both the text and background for subitems that have negative values.

For the complete example, see the DrawListViewSubItemEventArgs overview 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

Use this method to draw the subitem text using the current values of the ListViewItem.ListViewSubItem.ForeColor and ListViewItem.ListViewSubItem.Font properties. The text is drawn within the area specified by the Bounds property. The TextFormatFlags values specified in the flags parameter let you provide formatting properties for the node label, such as text alignment.

Note

The UseItemStyleForSubItems property of the parent ListViewItem must be set to false to prevent the ListViewItem.ForeColor and ListViewItem.Font values of the parent item from overriding the subitem values.

See also

Applies to