ListView with file extension icons

V

Víctor

I'm doing a kind of file explorer with some additional funcionalities. The
problem is that I'm not able to draw file extension icons in a ListView. I
know how obtain icons, but when I try to draw file icon, this isn't showed.

I tried to do by two ways:

1- By creating the ListViewItem and adding it to ListView, then I get
rectangle of icon element and paint a new icon in rectangle coordenates. I'm
able to view icons while they are drawn, but when all ListViewItems has been
added, icons dissapear and each ListViewItem hasn't icon or isn't visible.

2- Adding a void ImageList and inserting icons dinamically. While file list
is read, I check if file icon exists in ImageList, if icon exists I do
nothing, else I insert icon in ImageList. After I get index of image
inserted and set to ListViewItem ImageIndex attribute. When I execute a
System.NotSupportedException is thrown at line 08 of following code:

01. foreach(System.IO.FileInfo fi in di.GetFiles())
02. {

03. System.Drawing.Icon ico;

04. System.Drawing.Image ima;

05. lvi=new ListViewItem(fi.Name);

06. ico=ExtractIconClass.GetIconImage(fi.FullName);

07. ima=ico.ToBitmap();

08. if(!imgIcon.Images.Contains(ima))

09. {

10. imgIcon.Images.Add(ico.ToBitmap());

11. }

12. lvi.ImageIndex=imgIcon.Images.IndexOf(ico.ToBitmap());

13. lv.Items.Add(lvi); //addListViewItem to ListView

14. }

I don't know what is the problem. I prefer first method or if somebody knows
better I would like it know.

Thanks.
Victor
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

Wouldn't it be better to cache all known icons beforehand in the ImageList?
I believe they all are enumerated in the registry, and it should also
improve the performance of populating the file list as you won't need to
load the icons on the fly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top