Images in ListView subitem
Asked Answered
M

2

5

How to add an image instead of text for a listview subitem? Like http://i44.tinypic.com/2mzz6s6.png ?

here is normally how I add a string:

ListViewItem item = new ListViewItem("A");
item.SubItems.Add("B");
item.SubItems.Add("C");
listView1.Items.AddRange(new ListViewItem[] { item });

I am using .NET 2.0 with WinForms. Also, please don't link me to articles with superior .NET listview controls. I want to stick to the standard one in MSVC.

Muskmelon answered 26/11, 2011 at 4:13 Comment(0)
B
10

If you need the icon to be in the first column, then it could be easily done by creating an ImageList with images you want to display, assigning it to the SmallImageList property of the ListView and setting appropriate ImageIndex for the item. Like this:

listView1.SmallImageList = YourImageList;
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add("A");
lvi.SubItems.Add("B");
lvi.SubItems.Add("C");
lvi.ImageIndex = 2; // this will display YourImageList.Images[2] in the first column
listView1.Items.Add(lvi);
Buccal answered 26/11, 2011 at 5:10 Comment(2)
Thank you. A very elegant solution.Muskmelon
@pseudonym27, if you want icons to be displayed in LargeIcons mode, you should also assign an ImageList (YourImageList in the example above) to the LargeImageList property of the ListView.Buccal
X
-3

In WPF, you can do that easily,

In Windows Form, you can read

http://www.codeproject.com/KB/combobox/imagelistbox.aspx

as your sample.

The main idea is

First, set your listbox to ownerdraw

Then, in the itemdraw event (or is it drawitem?) grab your image and draw it in the space allotted (e.Graphics.DrawImage), then draw the test of the item next to it (e.Graphics.DrawString)

Xanthine answered 26/11, 2011 at 4:26 Comment(2)
You should read the ImageListBox's code and write your own. NoWay for you show Image in your ListBox without customize your ListBox (WinForm)Xanthine
The quiestion is about ListView not ListBox.Buccal

© 2022 - 2024 — McMap. All rights reserved.