ListView.DrawSubItem 事件

定義

當已繪製 ListView 的詳細資料檢視而且 OwnerDraw 屬性設定為 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 

事件類型

範例

下列程式碼範例提供事件處理常式的實作 DrawSubItem 。 如需完整的範例,請參閱 OwnerDraw 參考主題。

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

備註

此事件可讓您使用擁有者繪圖自訂控制項的外觀 ListView 。 只有當 屬性設定為 trueView 屬性設定為 View.DetailsOwnerDraw ,才會引發這個屬性。 如需擁有者繪圖的詳細資訊,請參閱 OwnerDraw 屬性參考主題。

注意

子專案資訊通常會出現在磚檢視和詳細資料檢視中,但在磚檢視中,它必須繪製在 事件的處理常式中 DrawItem

每個 ListView 子專案都可以發生此 DrawSubItem 事件。 您可以處理 DrawItem 事件來繪製所有子專案通用的專案,例如背景,並處理 DrawSubItem 事件來繪製個別子專案的元素,例如文字值。 您也可以只使用兩個事件之一來繪製 控制項中的所有 ListView 專案,但這可能較不方便。 若要在詳細資料檢視中繪製資料行標頭,您必須處理 DrawColumnHeader 事件。

注意

未將物件新增至 Columns 集合的任何子專案 ColumnHeader 都不會發生此 DrawSubItem 事件。 另請注意,每個 ListViewItem 物件的第一個子專案代表父專案本身,並顯示在第一個資料行中。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱