vb.net:imagelist loses images when the image size property is changed

P

Peter Ho

Hello,

does anyone know why my imagelist loses its entire imagecollection when I
change the imagesize property of the imagelist during run-time? This does
not happen when I change the imagesize property during design time. Does
anyone know a way to work around this problem?

kind regards,

Peter
 
C

Carl Prothman [MVP]

Peter,
It appears that if you set the ImageList's ImageSize property and Images collection
at design time, you won't be able to change the ImageSize without recreating the
Images collection as well.

I recommed you create a resource file, then add your images to that resource file,
then read the images from the resource (which gets embedded in your assembly)
at runtime.

Here is are great article on how to do this:
http://www.jelovic.com/articles/resources_in_visual_studio.htm
- Note, once you add the images to the resource file (using Resourcer tool)
make sure to *rename* them since the default name includes the path.

Here is the VB.NET code that I used to resize the ImageList and add
two images at runtime.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.ImageList1.ImageSize = New Size(16, 16) ' This will reset the Images collection

' Create a resource manager to retrieve images embedded in the assembly
Dim resManager As New System.Resources.ResourceManager("ImageList.Form1", _
System.Reflection.Assembly.GetExecutingAssembly())

' Add image1 to ImageList's Images collection
Dim myImage As Bitmap = CType(resManager.GetObject("Image1.jpg"), Bitmap)
Me.ImageList1.Images.Add(myImage)

' Add image2 to ImageList's Images collection
myImage = CType(resManager.GetObject("Image2.jpg"), Bitmap)
Me.ImageList1.Images.Add(myImage)
End Sub

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top