Hi,
I am designing a form in which there are some buttons created dynamically and each button has an image associated with it. The images are taken from an imagelist. During the application start up, the imagelist is loaded dynamically. My application needs to extract icons from the imagelist based on the icon name.The imagelist is to be searched and the index number of the particular icon is obtained
I have tried to use the the getindex() method so that I 'll be able to get the index number of the desired icon
the code looks like
i = ImageList1.Images.IndexOf(img)
But this is not working. It says getindex method is not supported.
Is there any other way to achieve the same functionality using imagelists

Obtaining images from Imagelists based on image name
SonK
I havent look at the VS2005 documentation ... I make my sample based on .Net 1.1. Sorry for the mistake.
Bye from Spain
El Bruno
PD: Sorry again :D
herriojr
What version are you using coz it definitely worked for me...
This is the overload:
Overloads Public Sub Add(
)ByVal key As String,
ByVal icon As Icon
check it out:
http://msdn2.microsoft.com/library/c3178y9h(en-us,vs.80).aspx
cheers,
Paul June A. Domag
sakotze
The ImageList does not support this Add Method overloads.
Another solution is to create your custom ImageList, for example one that contains a SortedList with all the keys and the Images. Here goes the sample class
Namespace
UtilsPublic Class ImageListEx
Inherits System.Windows.Forms.Control
Private WithEvents mImgLst As New System.Windows.Forms.ImageList
Private mSL As New SortedList
Public Property ImageList() As ImageList
Get
Return mImgLst
End Get
Set(ByVal Value As ImageList)
mImgLst = Value
End Set
End Property
Public Sub Add(ByVal key As String, ByVal image As System.Drawing.Image)
Dim i As Integer
i = mImgLst.Images.Add(image, System.Drawing.Color.Magenta)
mSL.Add(key, i)
End Sub
Public Function GetImage(ByVal key As String) As System.Drawing.Image
Dim i As Integer
If mSL.ContainsKey(key) Then
i = mSL.Item(key)
Return mImgLst.Images(i)
End If
End Function
End Class
End Namespace
(You must implements the Remove() functions, the Clear(), etc.)
After this you can use this control in a form. For example:
Private mImageList As New Utils.ImageListEx
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim img As System.Drawing.Bitmapimg =
New System.Drawing.Bitmap("c:\Img1.gif")mImageList.Add("Img1", img)
img =
New System.Drawing.Bitmap("c:\Img2.gif")mImageList.Add("Img2", img)
Me.PictureBox1.Image = mImageList.GetImage("Img2") End SubGood luck from Spain
El Bruno
Sorry 4 my poor English
G.L.
Try this,
In your Constructor (Or Application Startup) load the icons this way...
img1.Images.Add("myKey", Drawing.Icon("C:\\myIcon.ico"))
When you want to load the icon to a button,
button2.Image = img1.Images("myKey")
cheers,
Paul June A. Domag