Add Image And Text In Win-Form List Box

In the below section we will see how to add image and text inside the list box control.

In the below section we will see how to add image and text inside the list box control.

Important points

  1. ImageList control: we will use this control as a data source of images.
  2. ListBox control: Inside this control, we will display image and text.
  3. DrawItem event of the listbox which we will use to set image into listbox.

     

Steps to add image and text inside list box control of win-form,

Step 1

Drag and drop two controls "List Box" and "Image List".

Step 2

Add images into "Image List" control.

Image List control -> Properties -> Images -> Collection.

Add images inside this collection and give a name to each added image.

Step 3

Create data table which will fill the list box data source.

  1. public DataTableGetTable() {  
  2.     // Here we create a DataTable with four columns.  
  3.     DataTable table = new DataTable();  
  4.     table.Columns.Add("Id"typeof(int));  
  5.     table.Columns.Add("Data"typeof(string));  
  6.     // Here we add 6 DataRows.  
  7.     table.Rows.Add(25, "Paint Data");  
  8.     table.Rows.Add(26, "Contact Data");  
  9.     table.Rows.Add(27, "find Data");  
  10.     table.Rows.Add(28, "Paint Data");  
  11.     table.Rows.Add(29, "Contact Data");  
  12.     table.Rows.Add(30, "find Data");  
  13.     return table;  
  14. }  

Step 4

Bind the List box with data table.

  1. // Bind the List Box:  
  2. lstBoxImageAndText.DataSource = GetTable();  
  3. lstBoxImageAndText.DisplayMember = "Data";  
  4. lstBoxImageAndText.ValueMember = "Id";  

Step 5

User DrawItemevent of List Box to place an image with text data into Listbox.

Here, we will ImageList control to get the image,

  1. private void lstBoxImageAndText_DrawItem(object sender, DrawItemEventArgs e) {  
  2.     // Get row of List Box  
  3.     DataRowViewdrOfListBox = (DataRowView) lstBoxImageAndText.Items[e.Index];  
  4.     e.DrawBackground();  
  5.     Graphics g = e.Graphics;  
  6.     Rectangle rec = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);  
  7.     // Code to check the "Data" field value and on the bases of data  
  8.     // If data have "paint" then select image "Print"  
  9.     // If data have "Contact" then select image "Contact"  
  10.     if (drOfListBox["Data"].ToString().Contains("Paint")) {  
  11.         // imageList1 is the imageList control where we have added three images "Contact", //Search" and "Print"  
  12.         // DrawImage into List Box item  
  13.         g.DrawImage(imageList1.Images["Print"], rec);  
  14.     } else if (drOfListBox["Data"].ToString().Contains("Contact")) {  
  15.         g.DrawImage(imageList1.Images["Contact"], rec);  
  16.     } else if (drOfListBox["Data"].ToString().Contains("find")) {  
  17.         g.DrawImage(imageList1.Images["Search"], rec);  
  18.     }  
  19.     // drawstring into ListBox item  
  20.     Point p = new Point(e.Bounds.X + e.Bounds.Height + 2, e.Bounds.Y + 3);  
  21.     e.Graphics.DrawString(drOfListBox["Data"].ToString(), e.Font, new SolidBrush(Color.Black), p);  
  22. }