Redigera

Dela via


DrawListViewItemEventArgs.DrawText Method

Definition

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

Overloads

DrawText()

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

DrawText(TextFormatFlags)

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

DrawText()

Source:
DrawListViewItemEventArgs.cs
Source:
DrawListViewItemEventArgs.cs
Source:
DrawListViewItemEventArgs.cs

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

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

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.DrawItem event draws the background for entire items. In all views except the details view, this handler also draws the foreground text. In the details view, the foreground text is drawn in the ListView.DrawSubItem event.

For the complete example, see the DrawListViewItemEventArgs overview reference topic.

// Draws the backgrounds for entire ListView items.
private void listView1_DrawItem(object sender,
    DrawListViewItemEventArgs e)
{
    if ((e.State & ListViewItemStates.Selected) != 0)
    {
        // Draw the background and focus rectangle for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
        e.DrawFocusRectangle();
    }
    else
    {
        // Draw the background for an unselected item.
        using (LinearGradientBrush brush =
            new LinearGradientBrush(e.Bounds, Color.Orange,
            Color.Maroon, LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(brush, e.Bounds);
        }
    }

    // Draw the item text for views other than the Details view.
    if (listView1.View != View.Details)
    {
        e.DrawText();
    }
}
' Draws the backgrounds for entire ListView items.
Private Sub listView1_DrawItem(ByVal sender As Object, _
    ByVal e As DrawListViewItemEventArgs) _
    Handles listView1.DrawItem

    If Not (e.State And ListViewItemStates.Selected) = 0 Then

        ' Draw the background for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds)
        e.DrawFocusRectangle()

    Else

        ' Draw the background for an unselected item.
        Dim brush As New LinearGradientBrush(e.Bounds, Color.Orange, _
            Color.Maroon, LinearGradientMode.Horizontal)
        Try
            e.Graphics.FillRectangle(brush, e.Bounds)
        Finally
            brush.Dispose()
        End Try

    End If

    ' Draw the item text for views other than the Details view.
    If Not Me.listView1.View = View.Details Then
        e.DrawText()
    End If

End Sub

Remarks

Use this method to draw the item text using the current values of the ListViewItem.ForeColor and ListViewItem.Font properties. The text is drawn within the area specified by the Bounds property. You can also draw the text manually using the Graphics retrieved through the Graphics property.

The DrawText method is most useful when the ListView.View property is set to a value other than View.Details. In the details view, this method draws the text for the ListViewItem, but does not draw the text for subitems. Therefore, in the details view, it is generally more convenient to draw all of the ListView text in a handler for the ListView.DrawSubItem event.

See also

Applies to

DrawText(TextFormatFlags)

Source:
DrawListViewItemEventArgs.cs
Source:
DrawListViewItemEventArgs.cs
Source:
DrawListViewItemEventArgs.cs

Draws the text of the ListViewItem 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.

Remarks

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

The DrawText method is most useful when the ListView.View property is set to a value other than View.Details. In the details view, this method draws the text for the ListViewItem, but does not draw the text for subitems. Therefore, in the details view, it is generally more convenient to draw all of the ListView text in a handler for the ListView.DrawSubItem event.

See also

Applies to