ListView.DrawSubItem ListView.DrawSubItem ListView.DrawSubItem ListView.DrawSubItem Event

定義

ListView の詳細ビューが描画され、OwnerDraw プロパティが true に設定されている場合に発生します。Occurs when the details view of a ListView is drawn and the OwnerDraw property is set to true.

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イベントハンドラーの実装を示します。The following code example provides an implementation of a DrawSubItem event handler. 完全な例についてはOwnerDraw 、リファレンストピックを参照してください。For the complete example, see the OwnerDraw reference topic.

// 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を使用してコントロールの外観をカスタマイズできます。This event lets you customize the appearance of a ListView control using owner drawing. OwnerDrawこの値は、プロパティがにtrue View設定され、プロパティがにView.Details設定されている場合にのみ発生します。It is raised only when the OwnerDraw property is set to true and the View property is set to View.Details. オーナー描画の詳細については、 OwnerDraw 「プロパティのリファレンス」を参照してください。For more information on owner drawing, see the OwnerDraw property reference topic.

注意

通常、サブ項目の情報は、タイルビューだけでなく、詳細ビューにも表示されます。ただし、タイルビューでは、 DrawItemイベントのハンドラーに描画する必要があります。Subitem information typically appears in the tile view as well as the details view, but in the tile view, it must be drawn in a handler for the DrawItem event.

DrawSubItemListViewサブアイテムに対してイベントが発生する可能性があります。The DrawSubItem event can occur for each ListView subitem. DrawItemイベントを処理して、背景などのすべてのサブ項目に共通する要素を描画し、 DrawSubItemイベントを処理して個々のサブ項目の要素 (テキスト値など) を描画することができます。You can handle the DrawItem event to draw elements common to all subitems, such as the background, and handle the DrawSubItem event to draw elements for individual subitems, such as text values. 2つのイベントのうちの 1 ListViewつだけを使用して、コントロール内のすべての項目を描画することもできます。ただし、これは便利ではありません。You can also draw all items in the ListView control using only one of the two events, although this may be less convenient. 詳細ビューで列ヘッダーを描画するには、 DrawColumnHeaderイベントを処理する必要があります。To draw column headers in the details view, you must handle the DrawColumnHeader event.

注意

オブジェクトDrawSubItem ColumnHeaderがコレクションに追加されていないサブ項目に対しては、イベントは発生しません。ColumnsThe DrawSubItem event does not occur for any subitems for which no ColumnHeader object has been added to the Columns collection. また、各ListViewItemオブジェクトの最初のサブ項目が親項目自体を表し、最初の列に表示されることにも注意してください。Note also that the first subitem of each ListViewItem object represents the parent item itself, and is displayed in the first column.

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。For more information about handling events, see Handling and Raising Events.

適用対象

こちらもご覧ください