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プロパティに設定されてtrueViewプロパティに設定されて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.

DrawSubItemごとにイベントが発生する可能性がListViewサブ項目。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. すべての項目を描画することも、ListViewはあまり便利なことが考えられますが、2 つのイベントのいずれかのみを使用を制御します。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オブジェクトが追加されました、Columnsコレクション。The 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.

適用対象

こちらもご覧ください