Question Help. Can't assign an array of images to an ImageList.

Mugsy

Member
Joined
Sep 30, 2020
Messages
16
Programming Experience
10+
I'm trying to sort images before assigning them to an ImageList object.

Using a "For... Each" loop, I can assign images one at a time as they are read directly from the computer.

The problem is, this method reads the files alphabetically, so it reads "image09.png", "image10.png", "image100.png", "image101.png", "image102.png", etc before "image11.png" (physically renumbering the users files is not an option.)

My "solution" was to read the images into an array of type Image, inserting images into the array in order, then assign that array to my "ImageList" object.

This results in an assignment error, and I have no idea why. :cry:

Can't assign image array to an ImageList (error on line 11):
Dim imgImageArray(200) As Image
...
For Each ClosetFile As String In IO.Directory.GetFiles(strClosetPath, strCategory & "*.png", SortOrder.Ascending)
...
       Dim img = Image.FromFile(ClosetFile)
       imgImageArray(intPosition) = img ' "intPosition" parsed from filename.
...
Next ' ending For/Each loop.

For intCnt = 1 To intTotalNumberOfFiles ' Load image array with sorted list.
    ImageList2.Images.Add(imgImageArray(intCnt))
    ' also tried: ".AddRange" w/o the loop.
Next

Is there any other way to assign images to an ImageList control other than directly from disk?

TIA
 
Last edited:
Simply assigning data to an "ImageList" from a source other than direct storage shouldn't be this difficult.
It isn't difficult. You call Add and pass an Image or else call AddRange and pass an Image array. That's it, that's all. How you get those Image objects is up to you. I've shown you how to do that for an array of file paths in five lines of code. If you want to do it from some other source then you might describe that source and we can deal with that. I haven't looked back through this thread but I don't think you've specified any source other than files in a folder. I just don't know what problem we're supposed to be solving now. I don't think I'll post further until that is clear to me.
 
Let's assume for a moment that I'm not an idiot and already tried that.
If you tried it then either it worked, it failed to compile and generated a compilation error message or it compiled and then generated a run time exception message. I've asked you several times to post the error message and yet you still haven't. If I assume that you're not an idiot then I have to assume that you're withholding that information on purpose. Why would I waste any more time on this? I want to help you but you won't help me do so I'm out.
 
If you tried it then either it worked, it failed to compile and generated a compilation error message or it compiled and then generated a run time exception message. I've asked you several times to post the error message and yet you still haven't. If I assume that you're not an idiot then I have to assume that you're withholding that information on purpose. Why would I waste any more time on this? I want to help you but you won't help me do so I'm out.

See original question, Lines 10-13 of the sample code.
Using ".Add" (and ".AddRange") both resulted in syntax errors (underlined in red.)

I've since changed my code trying to assign a sorted array of images to ImageList2:

VB.NET:
   For intCnt = 1 To intTotalNumberOfFiles             ' Load image array with sorted list.
        ImageList2.Images.Item(intCnt) = imgImageArray(intCnt)           ' was = GetImages() but it didn't work. "GetImages()" gets redlined.
   Next

' Your function, now vestigial.
    Function GetImages() As ImageList
        Dim img As New ImageList
        For Each kvp As KeyValuePair(Of Integer, String) In dic
            img.Images.Add(kvp.Key, Image.FromFile(kvp.Value))
        Next
        Return img
    End Function

...but this also does not work ("imgImageArray" contains the image data, but the result is an empty "ImageList2" (as verified by setting a breakpoint.)
 
Back
Top