Share via


DrawListViewSubItemEventArgs.DrawText Metodo

Definizione

Crea il testo di ListViewItem.ListViewSubItem utilizzandone il colore di primo piano corrente e la formattazione predefinita.

Overload

DrawText()

Crea il testo di ListViewItem.ListViewSubItem utilizzandone il colore di primo piano corrente.

DrawText(TextFormatFlags)

Crea il testo di ListViewItem.ListViewSubItem utilizzando il relativo colore di primo piano corrente e i valori di formattazione TextFormatFlags specificati.

DrawText()

Crea il testo di ListViewItem.ListViewSubItem utilizzandone il colore di primo piano corrente.

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

Commenti

Utilizzare questo metodo per disegnare il testo dell'elemento ListViewItem.ListViewSubItem.ForeColor secondario utilizzando i valori correnti delle proprietà e ListViewItem.ListViewSubItem.Font . Il testo viene disegnato all'interno dell'area specificata dalla Bounds proprietà .

Nota

La UseItemStyleForSubItems proprietà dell'elemento padre ListViewItem deve essere impostata su false per impedire ai ListViewItem.ForeColor valori e ListViewItem.Font dell'elemento padre di eseguire l'override dei valori dell'elemento secondario.

Vedi anche

Si applica a

DrawText(TextFormatFlags)

Crea il testo di ListViewItem.ListViewSubItem utilizzando il relativo colore di primo piano corrente e i valori di formattazione TextFormatFlags specificati.

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)

Parametri

flags
TextFormatFlags

Combinazione bit per bit dei valori di TextFormatFlags.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare il DrawText metodo in un'applicazione che fornisce disegno personalizzato per un ListView controllo . Nell'esempio, un gestore per l'evento disegna i valori di testo dell'elemento ListView.DrawSubItem secondario e sia il testo che lo sfondo per gli elementi secondari con valori negativi.

Per l'esempio completo, vedere l'argomento di DrawListViewSubItemEventArgs riferimento di panoramica.

// 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

Commenti

Utilizzare questo metodo per disegnare il testo dell'elemento ListViewItem.ListViewSubItem.ForeColor secondario utilizzando i valori correnti delle proprietà e ListViewItem.ListViewSubItem.Font . Il testo viene disegnato all'interno dell'area specificata dalla Bounds proprietà . I TextFormatFlags valori specificati nel flags parametro consentono di specificare le proprietà di formattazione per l'etichetta del nodo, ad esempio l'allineamento del testo.

Nota

La UseItemStyleForSubItems proprietà dell'elemento padre ListViewItem deve essere impostata su false per impedire ai ListViewItem.ForeColor valori e ListViewItem.Font dell'elemento padre di eseguire l'override dei valori dell'elemento secondario.

Vedi anche

Si applica a