ListView.DrawSubItem イベントとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ListView.DrawSubItem イベントの意味・解説 

ListView.DrawSubItem イベント

メモ : このイベントは、.NET Framework version 2.0 で新しく追加されたものです。

ListView の詳細ビューが描画され、OwnerDraw プロパティが true に設定されている場合に発生します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Event DrawSubItem As
 DrawListViewSubItemEventHandler
Dim instance As ListView
Dim handler As DrawListViewSubItemEventHandler

AddHandler instance.DrawSubItem, handler
public event DrawListViewSubItemEventHandler DrawSubItem
public:
event DrawListViewSubItemEventHandler^ DrawSubItem {
    void add (DrawListViewSubItemEventHandler^ value);
    void remove (DrawListViewSubItemEventHandler^ value);
}
/** @event */
public void add_DrawSubItem (DrawListViewSubItemEventHandler
 value)

/** @event */
public void remove_DrawSubItem (DrawListViewSubItemEventHandler
 value)
JScript では、イベントは使用できますが、新規に宣言することはできません。
解説解説

このイベントを利用すると、オーナー描画を使用して ListView コントロールの外観をカスタマイズできます。これは、OwnerDraw プロパティが true に設定され、View プロパティが View.Details に設定されている場合にだけ発生します。オーナー描画の詳細については、OwnerDraw プロパティのリファレンス トピックを参照してください。

メモメモ

通常、サブ項目の情報は、詳細ビューだけでなくタイル ビューにも表示されますが、タイル ビューの場合は DrawItem イベントのハンドラで描画する必要があります。

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

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

使用例使用例

DrawSubItem イベント ハンドラの実装を提供するコード例を次に示します。詳細については、OwnerDraw のリファレンス トピックを参照してください。

' 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
// 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:
   void listView1_DrawSubItem( Object^ /*sender*/, DrawListViewSubItemEventArgs^
 e )
   {
      TextFormatFlags flags = TextFormatFlags::Left;
      StringFormat^ sf = gcnew StringFormat;
      try
      {
         // 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,
 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)
 == (ListViewItemStates)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 );
      }
      finally
      {
         if ( sf )
            delete (IDisposable^)sf;
      }
   }
// Draws subitem text and applies content-based formatting.
private void myListView_DrawSubItem(Object
 sender,
    DrawListViewSubItemEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Left;
    StringFormat sf = new StringFormat();
    try {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        if (e.get_Header().get_TextAlign().
            Equals(HorizontalAlignment.Center)) {

            sf.set_Alignment(StringAlignment.Center);
            flags = TextFormatFlags.HorizontalCenter;
        }
        else {
            if (e.get_Header().get_TextAlign().
                Equals(HorizontalAlignment.Right)) {

                sf.set_Alignment(StringAlignment.Far);
                flags = TextFormatFlags.Right;
            }
        }
        // Draw the text for a column header.
        if (e.get_ItemIndex() == -1) {
            Font myFont = new Font("Helvetica", 12,
 FontStyle.Bold);
            try {
                e.get_Graphics().DrawString(e.get_Item().get_Text(), 
                    myFont, Brushes.get_White(), 
                    new PointF((float)e.get_Bounds().get_X(),
 
                    (float)e.get_Bounds().get_Y()), sf);
            }
            finally {
                myFont.Dispose();
            }
            return;
        }

        // Draw the text and background for a subitem with a 
        // negative value. 
        double subItemValue = 0;
        if (e.get_ColumnIndex() > 0 && System.Double.TryParse(
            e.get_Item().get_SubItems().get_Item(e.get_ColumnIndex()).
            get_Text(), NumberStyles.Currency, 
            NumberFormatInfo.get_CurrentInfo(), subItemValue) 
            && subItemValue < 0) {

            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if (Convert.ToInt32(e.get_ItemState() 
                & ListViewItemStates.Selected) == 0) {
                e.DrawBackground();
            }
            // Draw the subitem text in red to highlight it. 
            e.get_Graphics().DrawString(e.get_Item().get_SubItems().
                get_Item(e.get_ColumnIndex()).get_Text(), 
                ((ListView)sender).get_Font(), Brushes.get_Red(), 
                RectangleF.op_Implicit(e.get_Bounds()), sf);

            return;
        }
        // Draw normal text for a subitem with a nonnegative 
        // or nonnumerical value.
        e.DrawText(flags);
    }
    finally {
        sf.Dispose();
    }
} //myListView_DrawSubItem
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ListView クラス
ListView メンバ
System.Windows.Forms 名前空間
ColumnHeader クラス
DrawListViewSubItemEventArgs クラス
ListView.DrawItem イベント
ListView.DrawColumnHeader イベント
ListViewItem.ListViewSubItem
ListView.OwnerDraw プロパティ
ListView.View プロパティ
View
その他の技術情報
イベントとデリゲート



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ListView.DrawSubItem イベント」の関連用語

ListView.DrawSubItem イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ListView.DrawSubItem イベントのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS