Home  |  About  | Last |  Submit  |  Contact

Previous Question:  [2008] Sending Raw Packets  Visual Basic .NETNext Question:  Get Files of Recycle Bin  Visual Basic .NET
Question [2008] ListView LargeIcon vs SmallIcon ( VBForums Visual Basic .NET )
Updated: 2009-04-01 18:40:45 (3)
[2008] ListView LargeIcon vs SmallIcon


I have a ListView capable of displaying files in all available views (Large Icon, Small Icon, Tile, List, Details).

Now I am wondering how to properly use the SmallImagelist and LargeImagelist for the small and large icons...

The ListView is actually a modified listview (inheriting from ListView), using a modified ListViewItemCollection.
In this custom ListViewItemCollection I use the Add override function to add a few details to my ListViewItem:
vb.net Code:
  1. Public Overrides Function Add(ByVal value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem
  2.         Dim file As clsTexture = TryCast(value, clsTexture)
  3.         If file IsNot Nothing Then
  4.             If Not _owner.LargeImageList.Images.ContainsKey(file.LargeIconPtr.ToString) Then _owner.LargeImageList.Images.Add(file.LargeIconPtr.ToString, file.LargeIcon)
  5.             If Not _owner.SmallImageList.Images.ContainsKey(file.LargeIconPtr.ToString) Then _owner.SmallImageList.Images.Add(file.LargeIconPtr.ToString, file.LargeIcon)
  7.             'PROBLEM HERE
  8.             file.ImageKey = file.LargeIconPtr.ToString
  10.             Dim str As String
  11.             If file.IsStandard Then
  12.                 str = "Yes"
  13.             Else
  14.                 str = "No"
  15.             End If
  17.             file.SubItems.Add(str)
  18.             MyBase.Add(file)
  19.         End If
  20. End Function
clsTexture is just a class inheriting from ListViewItem that:
- Hosts a LargeIcon and a LargeIconPtr property
- Hosts a SmallIcon and a SmallIconPtr property

The LargeIcon and SmallIcon icons are determined using the SHFileInfo API (or something... not really relevent I think) which gets the icons associated with the filetype of the file.
The SHFileInfo API uses the Handle (pointer?) of a System Icon to determine the icon. I had the idea to use that Handle (ToString) as the Key for the Image in the Imagelist, so that I am not adding 50000 of the same icons to a single list. So I use the LargeIconPtr and SmallIconPtr properties as the Handle.

As you can see I therefore now have a LargeIcon + its Handle, which I store in the LargeImageList with the Key as the Handle, and I also have a SmallIcon + its Handle, which I store in the SmallImagelist with the Key as the Handle.

The obvious problem now: how to set the ImageKey property of the ListViewItem ( = file)...??

If can either set it to the handle.ToString of the LargeIcon handle or the SmallIcon handle...

Are they always the same?
Or am I going to run into trouble just setting it to the LargeIcon handle (or SmallIcon handle for that matter), because they might be different, resulting in a different icon in the Large Icons view then in the Small Icons view...??

Suppose for example we have an icon where the SmallIcon looks different than the LargeIcon (more detail for example). Suppose the SmallIconPtr = 0, and the LargeIconPtr = 1.

If I now set the ImageKey to "1", it will use the LargeIcon when the ListView uses the LargeImagelist (depends on its View), but it will use the WRONG ICON when the SmallImagelist is used (because there may be, and probably is, a very different icon associated with key "1" in the SmallImagelist!!)

How can I get around this problem?
I realize it is a pretty complex post to read and will probably not get very many answers, but I really want to make the most of my Listviews...

Of course, one work around is to just always use the LargeIcon which then gets scaled down automatically when the SmallImagelist is used but I am pretty sure there are icons that are very different in Large or Small view...

Answers: [2008] ListView LargeIcon vs SmallIcon ( VBForums Visual Basic .NET )
[2008] ListView LargeIcon vs SmallIcon

image 1 in the largeimagelist should have Key of "X" .... image 1 of the smallimagelist should have a Key of "X" .... the imageKey for the listViewItem should be set to "X" .... then no matter which view is in use (large icons or small)... it should remain in synch. If you set the ImageKey that doesn't exist in the smallimagelist, but does exist in the largeimagelist... you will get the icon in the large icon, but not the small icon..... the reverse it true too.



[2008] ListView LargeIcon vs SmallIcon

But how can I be sure of that?

Suppose the following:

1. I add a file that has the following icons:
- LargeIcon with Handle = "0"
- SmallIcon with Handle = "1"

2. My code will then add icon with handle "0" to LargeImagelist with Key = "0" (because no Key of "0" exists yet). It will add icon with handle "1" to SmallImagelist with Key = "1".

3. My code sets file.ImageKey = "1".

4. I add another file with:
- LargeIcon handle = "0"
- SmallIcon handle = "2"

5. My code will NOT add a new image to LargeImagelist (because it already contains the key "0"), but it WILL add the image "2" to the SmallImagelist with Key = "2".

6. My code sets the ImageKey to "2"

Now, I have a ListViewItem (the second file) that looks for icons with key = "2", of which only a SmallIcon exists! So it will always use the SmallIcon I presume...?? Not good, right?!

Perhaps it is not possible to have the configuration of handles I jsut described..?? I really don't know, that's why I'm asking. If the small icons and large icons always have the same handles for example that would get rid of my problem completely, but somehow I doubt it....


[2008] ListView LargeIcon vs SmallIcon

I think you need to re-read Techgnome's post again.

The LargeIcon, SmallIcon and File should all have the same 'key'. How you establish that, and what that key is, is up to you. It may mean you have to 'duplicate' icons *

* You aren't actually duplicating anything, just putting an additional 'key' into the IconList. You can have the same icon added to the ImageList a hundred times - there's still only one icon - the keys simply point to the actual icon.


Previous Question:  [2008] Sending Raw Packets  VBForums  Visual Basic .NETNext Question:  Get Files of Recycle Bin  VBForums  Visual Basic .NET

- Source: [2008] ListView LargeIcon vs SmallIcon VBForums Visual Basic .NET
- Previous Question: [2008] Sending Raw Packets VBForums Visual Basic .NET
- Next Question: Get Files of Recycle Bin VBForums Visual Basic .NET