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

ListBox.DrawItem イベント

オーナー描画 ListBox のビジュアルな部分を変更すると発生します。

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

Public Event DrawItem As
 DrawItemEventHandler
Dim instance As ListBox
Dim handler As DrawItemEventHandler

AddHandler instance.DrawItem, handler
public event DrawItemEventHandler DrawItem
public:
event DrawItemEventHandler^ DrawItem {
    void add (DrawItemEventHandler^ value);
    void remove (DrawItemEventHandler^ value);
}
/** @event */
public void add_DrawItem (DrawItemEventHandler
 value)

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

このイベントは、オーナー描画 ListBox で使用します。このイベントは、DrawMode プロパティが DrawMode.OwnerDrawFixed または DrawMode.OwnerDrawVariable に設定されている場合にだけ発生します。このイベントを使用すると、ListBox 内の項目を描画するために必要なタスクを実行できます。サイズが可変の項目がある場合 (DrawMode プロパティが DrawMode.OwnerDrawVariable に設定されている場合) は、項目を描画する前に、MeasureItem イベントが発生します。MeasureItem イベントのイベント ハンドラを作成すると、DrawItem イベントのイベント ハンドラで描画する項目のサイズを指定できます。

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

使用例使用例

オーナー描画の ListBox 項目を作成する方法を次のコード例に示します。このコードでは、DrawMode プロパティを使用して、描画する項目が固定サイズであることを示し、DrawItem イベントを使用して、ListBox への各項目の描画を実行しています。この例のコードでは、イベント ハンドラへのパラメータとして渡された DrawItemEventArgs クラスのプロパティとメソッドを使用して項目を描画しています。この例では、listBox1 という名前の ListBox コントロールがフォームに追加されており、DrawItem イベントがこの例のコードに定義されているイベント ハンドラで処理される必要があります。またこの例では、"Apple"、"Orange"、および "Plum" という 3 つのテキストを持つ項目が、この順序で ListBox に追加されている必要もあります。

Private Sub listBox1_DrawItem(ByVal
 sender As System.Object, ByVal e As
 System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
   ' Set the DrawMode property to draw fixed sized items.
   ListBox1.DrawMode = DrawMode.OwnerDrawFixed
   ' Draw the background of the ListBox control for each item.
   e.DrawBackground()
   ' Define the default color of the brush as black.
   Dim myBrush As Brush

   ' Determine the color of the brush to draw each item based on the
 index of the item to draw.
   Select Case (e.Index)
      Case 0
         myBrush = Brushes.Red
      Case 1
         myBrush = Brushes.Orange
      Case 2
         myBrush = Brushes.Purple
   End Select

   ' Draw the current item text based on the current Font and the custom
 brush settings.
   e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush, New
 RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
   ' If the ListBox has focus, draw a focus rectangle around the selected
 item.
   e.DrawFocusRectangle()
End Sub
private void listBox1_DrawItem(object sender,
 System.Windows.Forms.DrawItemEventArgs e)
{
   // Set the DrawMode property to draw fixed sized items.
   listBox1.DrawMode = DrawMode.OwnerDrawFixed;
   // Draw the background of the ListBox control for each item.
   e.DrawBackground();
   // Define the default color of the brush as black.
   Brush myBrush = Brushes.Black;

   // Determine the color of the brush to draw each item based on the
 index of the item to draw.
   switch (e.Index)
   {
      case 0:
         myBrush = Brushes.Red;
         break;
      case 1:
         myBrush = Brushes.Orange;
         break;
      case 2:
         myBrush = Brushes.Purple;
         break;
   }

   // Draw the current item text based on the current Font and the custom
 brush settings.
   e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush,e.Bounds
,StringFormat.GenericDefault);
   // If the ListBox has focus, draw a focus rectangle around the selected
 item.
   e.DrawFocusRectangle();
}
private:
   void listBox1_DrawItem( Object^ /*sender*/, System::Windows::Forms::DrawItemEventArgs^
 e )
   {
      // Set the DrawMode property to draw fixed sized items.
      listBox1->DrawMode = DrawMode::OwnerDrawFixed;

      // Draw the background of the ListBox control for each item.
      e->DrawBackground();

      // Define the default color of the brush as black.
      Brush^ myBrush = Brushes::Black;

      // Determine the color of the brush to draw each item based on the
 index of the item to draw.
      switch ( e->Index )
      {
         case 0:
            myBrush = Brushes::Red;
            break;

         case 1:
            myBrush = Brushes::Orange;
            break;

         case 2:
            myBrush = Brushes::Purple;
            break;
      }

      // Draw the current item text based on the current Font and the custom
 brush settings.
      e->Graphics->DrawString( listBox1->Items[ e->Index ]->ToString(),
 e->Font, myBrush, e->Bounds, StringFormat::GenericDefault );

      // If the ListBox has focus, draw a focus rectangle around the selected
 item.
      e->DrawFocusRectangle();
   }
private void listBox1_DrawItem(Object sender
,
    System.Windows.Forms.DrawItemEventArgs e)
{
    // Set the DrawMode property to draw fixed sized items.
    listBox1.set_DrawMode(DrawMode.OwnerDrawFixed);
    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Create a new Brush and initialize to a Black colored brush
    // by default.
    Brush myBrush = Brushes.get_Black();
    // Determine the color of the brush to draw each item based on the
    // index of the item to draw.
    switch (e.get_Index()) {
        case 0 :
            myBrush = Brushes.get_Red();
            break;
        case 1 :
            myBrush = Brushes.get_Orange();
            break;
        case 2 :
            myBrush = Brushes.get_Purple();
            break;
    }

    // Draw the current item text based on the current Font and the
 custom
    // brush settings.
    e.get_Graphics().DrawString(System.Convert.ToString(listBox1.
        get_Items().get_Item(e.get_Index())), e.get_Font(), myBrush,
        RectangleF.op_Implicit(e.get_Bounds()), StringFormat.
        get_GenericDefault());
    // If the ListBox has focus, draw a focus rectangle around the selected
    // item.
    e.DrawFocusRectangle();
} //listBox1_DrawItem
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

ListBox.DrawItem イベントのお隣キーワード

   

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



ListBox.DrawItem イベントのページの著作権

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

©2026 GRAS Group, Inc.RSS