Sdílet prostřednictvím


ListView.DrawSubItem Událost

Definice

Nastane při zobrazení podrobností o vykreslení ListView a OwnerDraw vlastnost je nastavena na true.

public:
 event System::Windows::Forms::DrawListViewSubItemEventHandler ^ DrawSubItem;
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 

Event Type

Příklady

Následující příklad kódu poskytuje implementaci DrawSubItem obslužné rutiny události. Úplný příklad najdete v referenčním OwnerDraw tématu.

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

Poznámky

Tato událost umožňuje přizpůsobit vzhled ListView ovládacího prvku pomocí výkresu vlastníka. Vyvolá se pouze v případech, OwnerDraw kdy je vlastnost nastavena true a View vlastnost je nastavena na View.Details. Další informace o výkresu vlastníka naleznete v tématu s referenčními OwnerDraw informacemi o vlastnosti.

Poznámka:

Informace o dílčích počtech se obvykle zobrazují v zobrazení dlaždic i v zobrazení podrobností, ale v zobrazení dlaždic musí být nakresleny v obslužné rutině DrawItem události.

K DrawSubItem události může dojít u každého ListView dílčího podsítě. Událost můžete zpracovat DrawItem tak, aby kreslily prvky společné pro všechny dílčí položky, jako je pozadí, a zpracovat DrawSubItem událost pro kreslení prvků pro jednotlivé dílčí položky, jako jsou textové hodnoty. Všechny položky v ovládacím ListView prvku můžete také kreslit pouze pomocí jedné ze dvou událostí, i když to může být méně pohodlné. Pokud chcete v zobrazení podrobností nakreslit záhlaví sloupců, musíte zpracovat DrawColumnHeader událost.

Poznámka:

K DrawSubItem události nedojde u žádných dílčích položek, pro které nebyl do kolekce přidán Columns žádný ColumnHeader objekt. Všimněte si také, že první podnabídka každého ListViewItem objektu představuje samotnou nadřazenou položku a je zobrazena v prvním sloupci.

Další informace o zpracování událostí naleznete v tématu Zpracování a vyvolávání událostí.

Platí pro

Viz také