Skip to main content
A newer version of this page is available. .

How to: Custom Paint ListBoxControl's Items

  • 2 minutes to read

The following sample code handles the BaseListBoxControl.DrawItem event to custom paint the items displayed within the ListBoxControl. The odd and even items are painted with different appearance settings. A specific painting is applied to the currently selected item.

The image below shows the result.

BaseListBoxControl - DrawItem

using DevExpress.XtraEditors;

private void listBoxControl1_DrawItem(object sender, ListBoxDrawItemEventArgs e) {
   Brush backBrush1 = new SolidBrush(Color.FromArgb(224, 251, 254));
   Brush backBrush2 = new SolidBrush(Color.FromArgb(198, 241, 249));
   Brush backBrush3 = new SolidBrush(Color.FromArgb(253, 192, 47));
   // declare field representing the text of the item being drawn
   string itemText = (sender as ListBoxControl).GetItemText(e.Index);
   if ((e.State & DrawItemState.Selected) != 0){
      e.Cache.FillRectangle(backBrush3, e.Bounds);
      ControlPaint.DrawBorder3D(e.Graphics, e.Bounds);
      e.Cache.DrawString(itemText, new Font(e.Appearance.Font.Name, 
        e.Appearance.Font.Size, FontStyle.Bold), new SolidBrush(Color.Black), 
        e.Bounds, e.Appearance.GetStringFormat());
      e.Handled = true;
      return;
   }
   if(e.Index % 2 == 0){
      e.Cache.FillRectangle(backBrush1, e.Bounds);
   }
   else{
      e.Cache.FillRectangle(backBrush2, e.Bounds);
   }
   e.Cache.DrawString(itemText, e.Appearance.Font, new SolidBrush(Color.Black), 
     e.Bounds, e.Appearance.GetStringFormat());
   e.Handled = true;
}