ComboBox::DrawItem Event


Occurs when a visual aspect of an owner-drawn ComboBox changes.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

event DrawItemEventHandler^ DrawItem {
	void add(DrawItemEventHandler^ value);
	void remove(DrawItemEventHandler^ value);

This event is used by an owner-drawn ComboBox. You can use this event to perform the tasks needed to draw items in the ComboBox. If you have a variable sized item (when the ComboBox::DrawMode property is set to the OwnerDrawVariable value of System.Windows.Forms::DrawMode), before drawing an item, the MeasureItem event is raised. You can create an event handler for the MeasureItem event to specify the size for the item that you are going to draw in your event handler for the DrawItem event.

For more information about handling events, see Handling and Raising Events.

The following code example demonstrates how to create an owner-drawn combo box by setting the DrawMode property to OwnerDrawnVariable and handling the DrawItem and MeasureItem events. It also demonstrates setting the DropDownWidth and DropDownStyle properties. To run the example, paste the following code in a form. Call the InitializeComboBox method in the form's constructor or Load event.

   System::Windows::Forms::ComboBox^ ComboBox1;

   array<String^>^ animals;

   // This method initializes the owner-drawn combo box.
   // The drop-down width is set much wider than the size of the combo box
   // to accomodate the large items in the list.  The drop-down style is set to 
   // ComboBox.DropDown, which requires the user to click on the arrow to 
   // see the list.
   void InitializeComboBox()
      this->ComboBox1 = gcnew ComboBox;
      this->ComboBox1->DrawMode = System::Windows::Forms::DrawMode::OwnerDrawVariable;
      this->ComboBox1->Location = System::Drawing::Point( 10, 20 );
      this->ComboBox1->Name = "ComboBox1";
      this->ComboBox1->Size = System::Drawing::Size( 100, 120 );
      this->ComboBox1->DropDownWidth = 250;
      this->ComboBox1->TabIndex = 0;
      this->ComboBox1->DropDownStyle = ComboBoxStyle::DropDown;
      animals = gcnew array<String^>{"Elephant","c r o c o d i l e","lion"};
      ComboBox1->DataSource = animals;
      this->Controls->Add( this->ComboBox1 );

      // Hook up the MeasureItem and DrawItem events
      this->ComboBox1->DrawItem +=
         gcnew DrawItemEventHandler( this, &Form1::ComboBox1_DrawItem );
      this->ComboBox1->MeasureItem +=
         gcnew MeasureItemEventHandler( this, &Form1::ComboBox1_MeasureItem );

   // If you set the Draw property to DrawMode.OwnerDrawVariable, 
   // you must handle the MeasureItem event. This event handler 
   // will set the height and width of each item before it is drawn. 
   void ComboBox1_MeasureItem( Object^ sender,
      System::Windows::Forms::MeasureItemEventArgs^ e )
      switch ( e->Index )
         case 0:
            e->ItemHeight = 45;
         case 1:
            e->ItemHeight = 20;
         case 2:
            e->ItemHeight = 35;
      e->ItemWidth = 260;

   // You must handle the DrawItem event for owner-drawn combo boxes.  
   // This event handler changes the color, size and font of an 
   // item based on its position in the array.
   void ComboBox1_DrawItem( Object^ sender,
      System::Windows::Forms::DrawItemEventArgs^ e )
      float size = 0;
      System::Drawing::Font^ myFont;
      FontFamily^ family = nullptr;

      System::Drawing::Color animalColor;
      switch ( e->Index )
         case 0:
            size = 30;
            animalColor = System::Drawing::Color::Gray;
            family = FontFamily::GenericSansSerif;
         case 1:
            size = 10;
            animalColor = System::Drawing::Color::LawnGreen;
            family = FontFamily::GenericMonospace;
         case 2:
            size = 15;
            animalColor = System::Drawing::Color::Tan;
            family = FontFamily::GenericSansSerif;

      // Draw the background of the item.

      // Create a square filled with the animals color. Vary the size
      // of the rectangle based on the length of the animals name.
      Rectangle rectangle = Rectangle( 2, e->Bounds.Top + 2,
         e->Bounds.Height, e->Bounds.Height - 4 );
      e->Graphics->FillRectangle( gcnew SolidBrush( animalColor ), rectangle );

      // Draw each string in the array, using a different size, color,
      // and font for each item.
      myFont = gcnew System::Drawing::Font( family, size, FontStyle::Bold );
      e->Graphics->DrawString( animals[ e->Index ], myFont,
         System::Drawing::Brushes::Black, RectangleF(
            e->Bounds.X + rectangle.Width, e->Bounds.Y,
            e->Bounds.Width, e->Bounds.Height) );

      // Draw the focus rectangle if the mouse hovers over an item.

.NET Framework
Available since 1.1
Return to top