How can I set an icon for a ListViewSubItem?
Asked Answered
T

7

14

In a ListView you can have icons on each item.
When viewing in Details-mode, the icon is shown in the left-most column.

Can I show an icon in some other column?

Tinsmith answered 11/12, 2009 at 14:48 Comment(0)
B
12

The ListView control does not support images in sub-items natively. The easiest thing to do is switch to a DataGridView and use a DataGridViewImageColumn. If that is not possible, then you'll need to draw the icons yourself using the custom draw support in the ListView control. To do this set ListView.OwnerDraw = true and handle the ListView.DrawSubItem and ListView.DrawColumnHeader events.

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    // Only interested in 2nd column.
    if (e.Header != this.columnHeader2)
    {
        e.DrawDefault = true;
        return;
    }

    e.DrawBackground();
    var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
    e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), imageRect);
}

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}
Baseline answered 11/12, 2009 at 16:22 Comment(3)
why not just var imageRect = e.Bounds; It is a value type so the values are going to be copied and not referenced make them safe to handle.Sneakbox
Exposition, in real world usage you might want to adjust imageRect to account for alignment, padding and/or additional content contained in the subitem.Baseline
This code does not draw selected background color under image for selected item.Otranto
P
8

Use P/Invoke and send LVM_SETITEM message to the listview (you should set LVS_EX_SUBITEMIMAGES style on control creation or via LVM_SETEXTENDEDLISTVIEWSTYLE), specify the subitem index and the corresponding image index. You will need to do it for every list item you insert.

Patter answered 11/12, 2009 at 16:38 Comment(0)
C
6

ObjectListView is an open source wrapper around a .NET Winforms ListView. It supports images on subitems using the p/invoke strategy that that @ligget78 mentioned. It also solves many other common problems with a ListView.

It allows you to make very nice looking ListViews with a minimum effort:

alt text
(source: sourceforge.net)

Cumulation answered 17/12, 2009 at 7:46 Comment(0)
S
2

Inherit from ListView and draw your own icons.

public class MyListView : ListView
{
    protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
    {
        base.OnDrawSubItem(e);
    }
}
Sacerdotalism answered 11/12, 2009 at 14:58 Comment(0)
V
1

The icon is shown in the "first" column, and this is also the basis for the keyboard prefix search. One possible solution could be to reorder the columns by setting the DisplayIndex of the first column to something else.

listView1.Columns[0].DisplayIndex = 1;

This of course only works if you need an icon in only one column.

Villalba answered 14/3, 2017 at 10:8 Comment(0)
G
0

There's no .NET support for this.

Have a look at this project.

Gastrotrich answered 11/12, 2009 at 14:55 Comment(0)
R
0

Take a loot at this:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d25b4ffa-2ea4-43cd-a3ae-8dd0387197ae/

In addition to the accepted answer, you should handle the DrawItem event as well, or it will not work.

Razid answered 12/11, 2012 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.