Export (0) Print
Expand All

ListBox.DrawMode Property

Gets or sets the drawing mode for the control.

[Visual Basic]
Public Overridable Property DrawMode As DrawMode
[C#]
public virtual DrawMode DrawMode {get; set;}
[C++]
public: __property virtual DrawMode get_DrawMode();
public: __property virtual void set_DrawMode(DrawMode);
[JScript]
public function get DrawMode() : DrawMode;
public function set DrawMode(DrawMode);

Property Value

One of the DrawMode values representing the mode for drawing the items of the control. The default is DrawMode.Normal.

Exceptions

Exception Type Condition
InvalidEnumArgumentException The value assigned to the property is not a member of the DrawMode enumeration.
ArgumentException A multicolumn ListBox cannot have a variable-sized height.

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

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

See Also

ListBox Class | ListBox Members | System.Windows.Forms Namespace | DrawMode | DrawItem

Show:
© 2014 Microsoft