Export (0) Print
Expand All
Expand Minimize

DrawMode Enumeration

Specifies how the elements of a control are drawn.

[Visual Basic]
<Serializable>
Public Enum DrawMode
[C#]
[Serializable]
public enum DrawMode
[C++]
[Serializable]
__value public enum DrawMode
[JScript]
public
   Serializable
enum DrawMode

Remarks

This enumeration is used by members such as DrawMode in the ListBox, CheckedListBox, and ComboBox classes.

You can override the drawing of some controls or certain elements. This enumeration is used to specify if a control is drawn by the operating system or if your own code handles the drawing of the control.

Note   The CheckedListBox class only supports DrawMode.Normal; owner draw modes are ignored.

For more information about using the DrawMode enumeration, see the MeasureItem and DrawItem events, and the ItemHeight property.

Members

Member name Description
Normal All the elements in a control are drawn by the operating system and are of the same size.
OwnerDrawFixed All the elements in the control are drawn manually and are of the same size.
OwnerDrawVariable All the elements in the control are drawn manually and can differ in size.

Example

[Visual Basic, C#, C++] The following example demonstrates how to create owner-drawn ListBox items. The code uses the DrawMode property to specify that the items drawn are fixed sized and the DrawItem event to perform the drawing of each item into the ListBox. The example code uses the properties and methods of the DrawItemEventArgs class passed as a parameter to the event handler to draw the items. This example assumes that a ListBox control called listBox1 has been added to a form and that the DrawItem event is handled by the event handler defined in the example code. The example also assumes that items have been added to the ListBox with the text of "Apple", "Orange", and "Plum" in that order.

[Visual Basic] 
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()
   ' Create a new Brush and initialize to a Black colored brush by default.
   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

[C#] 
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();
   // Create a new Brush and initialize to a Black colored brush by default.
   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();
}

[C++] 
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();
      // Create a new Brush and initialize to a Black colored brush by default.
      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->Item[e->Index]->ToString(),
         e->Font, myBrush,
         RectangleF::op_Implicit(e->Bounds),
         StringFormat::GenericDefault);
      // If the ListBox has focus, draw a focus rectangle around the selected item.
      e->DrawFocusRectangle();
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

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

See Also

System.Windows.Forms Namespace | ListBox | CheckedListBox | ComboBox

Show:
© 2014 Microsoft