Tough ImageList Question

J

Just Me

If I add an image to an imagelist using
imageList.Images.Add...
And use it with a tree node
tn.ImageIndex = imageList.Images.Count-1

This is the actual code:
Win32.Shell.SHGetFileInfo(NodePath, 256, fileinfo, cbFileInfo, flags)
tvFolders.ImageList.Images.Add(Icon.FromHandle(fileinfo.hIcon))
tn.ImageIndex = tvFolders.ImageList.Images.Count - 1

It may be that the image is already in the imagelist.
I'd like to check.
The following was suggested to get the index:
Index = imageList.Images.IndexOf(myImage)
But I find IndexOf is not supported.

The only thing I can think of is to save hIcon in an two column array along
with Count=1 and check before I add to the imagelist. But if the imagelist
can tell me if it's already in it I'd rather not have an extra data
structure around.

1)Anyone know how to check the ImageList??

2)If I must have the data structure is an array (hIcon,index) the best one?
 
K

Ken Tucker [MVP]

Hi,

Since the imagelist.images.contains is not support the best
suggestion I have is to create a hashtable. Store the index of icon in the
hashtable with the icon as the key. Use the hashtable.contains method to
see if you already added the icon. Quick example.


Private Declare Function ExtractIconEx Lib "shell32.dll" Alias
"ExtractIconExA" _

(ByVal lpszFile As String, ByVal nIconIndex As Integer, _

ByVal phiconLarge() As IntPtr, ByVal phiconSmall() As IntPtr, _

ByVal nIcons As Integer) As Integer

Dim hticons As New Hashtable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim arIcons As New ArrayList

Dim strPath As String

ImageList1.Images.Clear()

ImageList1.ImageSize = New Size(32, 32)

strPath = "C:\Windows\Explorer.exe"

arIcons = GetIcon(strPath)

Me.Text = strPath & " icons"

Dim clsIcon As clsIconsFromFile

Dim y As Integer = 0

For Each clsIcon In arIcons

ImageList1.Images.Add(clsIcon.Large)

hticons.Add(clsIcon.Large, y)

y += 1

Dim fs As New System.IO.FileStream(y.ToString & ".ico", IO.FileMode.Create)

clsIcon.Large.Save(fs)

fs.Close()

Next

Dim ico As Icon

ico = DirectCast(arIcons.Item(0), clsIconsFromFile).Large

Debug.WriteLine(hticons.ContainsKey(ico))

ListView1.Items.Clear()

Dim x As Integer



For x = 0 To ImageList1.Images.Count - 1

Dim lvi As New ListViewItem

lvi.ImageIndex = x

ListView1.Items.Add(lvi)

Next

End Sub

Private Function GetIcon(ByVal Path As String) As ArrayList

Dim Large() As IntPtr

Dim Small() As IntPtr

Dim i, x As Integer

Dim cIcon As clsIconsFromFile

Dim arIcons As ArrayList

i = ExtractIconEx(Path, -1, Large, Small, -1) ' get number of icons

ReDim Large(i - 1)

ReDim Small(i - 1)

i = ExtractIconEx(Path, 0, Large, Small, i)

arIcons = New ArrayList

For x = 0 To i - 1

cIcon = New clsIconsFromFile

cIcon.Large = Icon.FromHandle(Large(x))

cIcon.Small = Icon.FromHandle(Small(x))

arIcons.Add(cIcon)

Next x

Return arIcons

End Function



' Here is the helper class



Public Class clsIconsFromFile

Private m_icLarge As Icon

Private m_icSmall As Icon

Public Property Large() As Icon

Get

Return m_icLarge

End Get

Set(ByVal Value As Icon)

m_icLarge = Value

End Set

End Property

Public Property Small() As Icon

Get

Return m_icSmall

End Get

Set(ByVal Value As Icon)

m_icSmall = Value

End Set

End Property

End Class



Ken
--------------------------
 
J

Just Me

Ken Tucker said:
Hi,

Since the imagelist.images.contains is not support the best
suggestion I have is to create a hashtable. Store the index of icon in the
hashtable with the icon as the key. Use the hashtable.contains method to
see if you already added the icon. Quick example.

If this you did quickly I'd like to see what you can do given more time.

Thanks a lot. It helped.
 
J

Just Me

I replied before but do not see the reply.

Do you see it?

Anyway, thanks for the help.
 

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