Display Images in C1Combo

The C1Combo control can be used as a multicolumn drop-down list box as explained here. This blog discusses how C1Combo can be further customized to display images along with text both in the drop down list and after an item is selected from this list. ImageCombo All we need is to set the DrawMode property of C1Combo to OwnerDrawVariable so as to fire the DrawItem event and set the ComboStyle to DropdownList. Here is the code:

c1Combo1.ComboStyle = C1.Win.C1List.ComboStyleEnum.DropdownList;  
c1Combo1.DrawMode = DrawMode.OwnerDrawVariable;  

Once the properties are set, C1Combo is customized using the DrawItem event with the following code that draws a specific image at a specific location:

void c1Combo1_DrawItem(object sender, C1.Win.C1List.OwnerDrawCellEventArgs e)  
  string text = e.Text;  
  using (Brush brush = new SolidBrush(e.Style.BackColor))  
    e.Graphics.FillRectangle(brush, e.CellRect);  
  if (text.Length > 0)  
    string priority = text;  
    Image img = SystemIcons.Exclamation.ToBitmap();  
    if (img != null)  
       e.Graphics.DrawImage(img, e.CellRect.X, e.CellRect.Y, 15, 15);  
 e.Graphics.DrawString(text, c1Combo1.Font, System.Drawing.Brushes.Black, new RectangleF(e.CellRect.X + 15, e.CellRect.Y,e.CellRect.Width, e.CellRect.Height));  
 e.Handled = true;  

Refer to the attached sample for complete implementation. Download C# Sample Download VB Sample


GrapeCity Developer Tools
comments powered by Disqus