Owner Draw ListBox Control with Images

I was just looking around the code submitted by Sanjay Ahuja for the Owner Draw ListBox here: Owner Draw ListBox Control
 
And I started modifying the code. I found out there was a way to insert an icon in the Listbox control. This part of the code I have taken from Shripad Kulkarni's article Owner Draw Menus (including icons). All you have to do is to just follow the Procedure given by Sanjay Ahuja and start the project.
 
To make this simpler I have included the procedure here.
 
Note: Please copy all the bitmaps either to bin/debug or bin/release folder.
 
We start application by creating a Windows Application. Add a ListBox control to the form and set its DrawMode property to OwnerDrawVariable. Alternatively you can add following line to InitializeComponent() function of your form,
 
//lstColor is ListBox control
this.lstColor.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
 
Next add following lines after the above line
  1. //tell windows we are interested in drawing items in ListBox on our own  
  2. this.lstColor.DrawItem += new DrawItemEventHandler(this.DrawItemHandler);  
  3. //tell windows we are interested in providing item size  
  4. this.lstColor.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.MeasureItemHandler);  
By doing this, windows will send the DrawItem and MeasureItem events for each item added to ListBox.
 
Next, add handlers for these events
  1. private void DrawItemHandler(object sender, DrawItemEventArgs e)  
  2. {  
  3.     e.DrawBackground();  
  4.     e.DrawFocusRectangle();  
  5.     e.Graphics.DrawString(data[e.Index],new   
  6.     Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold),new SolidBrush(color[e.Index]),e.Bounds);  
  7. }  
  8.   
  9. private void MeasureItemHandler(object sender, MeasureItemEventArgs e)  
  10. {  
  11.     e.ItemHeight= 22;  
  12. }  
In above code, date is an array that holds items to be inserted and color is an array of class Color.
 
That's it. We are done!!! I think so NO!!!!
 
Here comes the fun part.
 
Include this in the Class part
  1. private string []data;  
  2. private string []data2;  
  3. private Color []color;  
  4. Bitmap img_fileopen , img_exit , img_close ;  
  5. Bitmap img_security , img_network , img_about;  
Include this in the Constructor
  1. data= new String[6]{"Open - Red","Close - Azure","Exit - Bisque","Security - BurlyWood","Network - Yellow","About - AntiqueWhite"};  
  2. data2=new String[5]{"First","Second","Third","Forth","Fifth"};  
  3. color=new Color[6]{Color.Red,Color.Azure,Color.Bisque,Color.BurlyWood,Color.Yellow,Color.AntiqueWhite};  
  4. listBox1.DataSource = data;  
  5. listBox2.DataSource = data2;  
  6. img_fileopen =new Bitmap("FileOpen.bmp");   
  7. img_exit =new Bitmap("exit.bmp");   
  8. img_close =new Bitmap("Close.bmp");  
  9. img_about =new Bitmap("about.bmp");  
  10. img_security =new Bitmap("security.bmp");   
  11. img_network =new Bitmap("network.bmp");  
For listBox handlers events. (see the source code for more details)
  1. Rectangle rc = new Rectangle(e.Bounds.X+1, e.Bounds.Y+1, e.Bounds.Width-5, e.Bounds.Height-3);  
  2. e.Graphics.FillRectangle(new SolidBrush(Color.CornflowerBlue), rc);  
  3. string []datas = data;  
  4. StringFormat sf =new StringFormat();  
  5. sf.Alignment = StringAlignment.Far;  
  6. e.Graphics.DrawString(datas[e.Index],new Font("Verdana", 10, FontStyle.Bold), new SolidBrush(color[e.Index]), rc, sf);  
  7. e.DrawFocusRectangle();  
  8. e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.White), 2), rc);  
To add the images (Please download the images). Add this code.
  1. Image useImage = null ;  
  2. if ( datas[e.Index] == "Open - Red" )   
  3. {  
  4.     useImage = img_fileopen;  
  5. }  
  6. if ( datas[e.Index] == "Close - Azure" )  
  7. {  
  8.     useImage = img_close;  
  9. }  
  10. if ( datas[e.Index] == "Exit - Bisque" )  
  11. {  
  12.     useImage = img_exit;  
  13. }  
  14. if ( datas[e.Index] == "Security - BurlyWood" )  
  15. {  
  16.     useImage = img_security;  
  17. }  
  18. if ( datas[e.Index] == "Network - Yellow" )  
  19. {  
  20.     useImage = img_network;  
  21. }  
  22. if ( datas[e.Index] == "About - AntiqueWhite" )  
  23. {  
  24.     useImage = img_about;  
  25. }  
  26. if ( useImage != null )   
  27. {  
  28.     SizeF sz = useImage.PhysicalDimension;  
  29.     e.Graphics.DrawImage(useImage, e.Bounds.X+5 , ( e.Bounds.Bottom + e.Bounds.Top ) /2 - sz.Height/2);  
  30. }
That's it. Have fun!!!!!