How can I include icons in my ListBox?

17,609

Solution 1

Will a ListView work for you? That is what I use. Much easier and you can make it look just like a ListBox. Also, plenty of documentation on MSDN to get started with.

How to: Display Icons for the Windows Forms ListView Control
The Windows Forms ListView control can display icons from three image lists. The List, Details, and SmallIcon views display images from the image list specified in the SmallImageList property. The LargeIcon view displays images from the image list specified in the LargeImageList property. A list view can also display an additional set of icons, set in the StateImageList property, next to the large or small icons. For more information about image lists, see ImageList Component (Windows Forms) and How to: Add or Remove Images with the Windows Forms ImageList Component.

Inserted from How to: Display Icons for the Windows Forms ListView Control

Solution 2

If you don't want to change ListBox to a ListView you can write a handler for DrawItemEvent. for example:

private void InitializeComponent()
{
    ...
    this.listBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox_DrawItem);
    ...
 }
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index == -1)
            return;
        // Draw the background of the ListBox control for each item.
        e.DrawBackground();
        var rect = new Rectangle(e.Bounds.X+10, e.Bounds.Y+8, 12, 14);
       //assuming the icon is already added to project resources
        e.Graphics.DrawIconUnstretched(YourProject.Properties.Resources.YouIcon, rect);
        e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
            e.Font, Brushes.Black, new Rectangle(e.Bounds.X + 25, e.Bounds.Y + 10, e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);
        // If the ListBox has focus, draw a focus rectangle around the selected item.
        e.DrawFocusRectangle();
    }

you can play around with the rectangle to set the location of the icon right

Solution 3

If you're stuck working in WinForms, then you'll have to owner-draw your items.

See the example for the DrawItem event.

Solution 4

A little different approach - don't use a list box. Instead of using that control that bounds me to its limited set of properties and methods I am making a listbox of my own.

It's not as hard as it sounds:

int yPos = 0;    
Panel myListBox = new Panel();
foreach (Object object in YourObjectList)
{
    Panel line = new Panel();
    line.Location = new Point(0, Ypos);
    line.Size = new Size(myListBox.Width, 20);
    line.MouseClick += new MouseEventHandler(line_MouseClick);
    myListBox.Controls.Add(line);

    // Add and arrange the controls you want in the line

    yPos += line.Height;
}

Example for myListBox event handlers - selecting a line:

private void line_MouseClick(object sender, MouseEventArgs)
{
    foreach (Control control in myListBox.Controls)
        if (control is Panel)
            if (control == sender)
                control.BackColor = Color.DarkBlue;
            else
                control.BackColor = Color.Transparent;      
}

The code samples above were not tested but the described method was used and found very convenient and simple.

Share:
17,609
user629926
Author by

user629926

Updated on June 28, 2022

Comments

  • user629926
    user629926 almost 2 years

    I know that similar questions have already been asked here before, but they all lead to the same codeproject article that doesn't work. Does anybody know of a working ListBox with icons?