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 外観をカスタマイズできます。 プロパティが に設定され、 OwnerDraw プロパティが にtrue設定されている場合にのみ発生しますView.DetailsView 所有者描画の詳細については、プロパティの参照に関するトピックを OwnerDraw 参照してください。

注意

サブアイテム情報は通常、タイル ビューと詳細ビューに表示されますが、タイル ビューでは、イベントのハンドラーに描画する DrawItem 必要があります。

イベントは DrawSubItem 、各 ListView サブ項目に対して発生する可能性があります。 イベントを DrawItem 処理して、背景などのすべてのサブ項目に共通する要素を描画し、イベントを DrawSubItem 処理してテキスト値などの個々のサブ項目の要素を描画できます。 また、2 つのイベントのうち 1 つだけを ListView 使用してコントロール内のすべての項目を描画することもできますが、これはあまり便利ではない場合があります。 詳細ビューで列ヘッダーを描画するには、 イベントを処理する DrawColumnHeader 必要があります。

注意

このイベントはDrawSubItem、オブジェクトがコレクションに追加ColumnsされていないColumnHeaderサブアイテムには発生しません。 また、各 ListViewItem オブジェクトの最初のサブ項目は親項目自体を表し、最初の列に表示されることにも注意してください。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

こちらもご覧ください