To Make the Combox customized with the Custom Object Items in it,
we have to implment the DrawItem event and add custom items in it.
Following example shows the ComboBox with Images in the Dropdown list:
1. Intialize
//
// cb_ImageList
//
private System.Windows.Forms.ComboBox cb_ImageList;
this.cb_ImageList = new System.Windows.Forms.ComboBox();
this.cb_ImageList.AllowDrop = true;
this.cb_ImageList.BackColor = System.Drawing.SystemColors.Menu;
this.cb_ImageList.Cursor = System.Windows.Forms.Cursors.Hand;
this.cb_ImageList.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.cb_ImageList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cb_ImageList.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.cb_ImageList.FormattingEnabled = true;
this.cb_ImageList.ItemHeight = 16;
this.cb_ImageList.Location = new System.Drawing.Point(512, 56);
this.cb_ImageList.Name = "cb_ImageList";
this.cb_ImageList.Size = new System.Drawing.Size(42, 22);
this.cb_ImageList.TabIndex = 7;
this.cb_ImageList.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.cb_ImageList_DrawItem)
2. Add some empty string items in the ComboBox and intialize Image Array
ArrayList imageList = newArrayList();
imageList.Add(
Resource1.warning);
imageList.Add(
Resource1.error);
this.cb_ImageList.Items.Add("");
this.cb_ImageList.Items.Add("");
3. Write DrawItem method
///<summary>
/// This event add the Custom Items in the Combobox
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
privatevoid cb_ImageList_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics _graphics = e.Graphics;
Rectangle _rectangle = e.Bounds;
System.Drawing.
Image bitmap;
if (e.Index >= 0)
{
switch (e.Index)
{
case 0:
bitmap =
newBitmap(imageList[0] asImage);
_graphics.DrawImage(bitmap, _rectangle);
break;
case 1:
bitmap =
newBitmap(imageList[1] asImage);
_graphics.DrawImage(bitmap, _rectangle);
break;
}
}
}