|
-
Aug 24th, 2024, 06:26 PM
#1
Thread Starter
Lively Member
Images loaded into an imagelist aren't displaying in ListView window.
Short version:
I'm reading png images into an ImageList which are then loaded into a ListView, but no images appear.
Long version:
I'm reading over 200 image files (eg: "File01.png"... "File10.png"... "File100.png", etc.) using a "For/Each loop"
Problem is, it reads "File100.png" before "File11.png", so my program was displaying them in the wrong order.
My "solution" was to extract the file number from the filename and plug the paths into an array using the file number for its position, then (once the For/Each loop has finished), I read the image at those paths into an ImageList and then assign the ImageList to a ListView. But when I try (using a For/Next loop after the array is filled), the ListView comes up empty and I have no idea why. 
Code:
Sub LoadImageList()
' Clear old items.
lsvCollection.Clear()
ImageList2.Images.Clear()
lsvCollection.Refresh()
Dim strTempSortBuffer(256) As String ' NEW. File100 comes before File11,
' so we must sort icons using an array.
' Read Items.
intCnt = 1
strTempSortBuffer.Initialize() ' NEW
For Each ClosetFile As String In IO.Directory.GetFiles(strClosetPath, strCategory & "*.png")
' Fill array in "File number" order.
strTempSortBuffer(Val(Mid(Path.GetFileNameWithoutExtension(ClosetFile.ToString), Len(strCategory) + 1))) = ClosetFile.ToString
' Old code: Dim img = Image.FromFile(strTempSortBuffer(intCnt)) ' pre-sorted array has gaps, so we must add images later AFTER array is filled.
' ImageList2.Images.Add((intCnt - 1).ToString, img)
' img.Dispose()
intCnt += 1
Next
' NEW code:
intTotalNumberOfFiles = intCnt - 1
For intCnt = 1 To intTotalNumberOfFiles
Dim img As Image = Image.FromFile(strTempSortBuffer(intCnt))
ImageList2.Images.Add(intCnt - 1, img)
img.Dispose() ' Tried WITH & WITHOUT this line.
Next
' after adding items to the listview, set the columns to autosize.
lsvCollection.Columns.Add("Column 1", 60, HorizontalAlignment.Left)
lsvCollection.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize)
lsvCollection.SmallImageList = ImageList2 ' Reassign to list with icons.
lsvCollection.View = View.Details
...
The old code "worked" (images appear) but in the wrong order after "File10". SAME code performed outside the ForEach loop results in NO images being displayed. Debugger says the image list has images in it. Paths are fine. I even tried using a hard coded path and it still didn't work.
Any ideas? I'm stumped. TIA
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|