By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

ListView with file extension icons

P: n/a
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

Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Víctor" <vi******@vodafone.es> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
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


Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.