assign index when import image in imagelist

User avatar
sal21
PlatinumLounger
Posts: 4354
Joined: 26 Apr 2010, 17:36

assign index when import image in imagelist

Post by sal21 »

Code: Select all

Private Sub FILL_IMAGELIST()

    Dim NOME As String, PATH As String
    PATH = "C:\Lavori_Vb6\HOTEL\IMG\REPARTI\"
    Dim FSO As New FileSystemObject
    Dim FIL As FILE

    Me.ImageList1.ListImages.Clear
    For Each FIL In FSO.GetFolder(PATH).Files
        NOME = FIL.Name
        STATO = Replace(NOME, ".jpg", "")
        Me.ImageList1.ListImages.Add , STATO, LoadPicture(PATH & NOME)
    Next

End Sub
I can assign an index when load image in imagelist.

For example in dir i can have:

1-CAFFETTERIA.jpg

I can assign the index 1 when add image?

User avatar
SpeakEasy
4StarLounger
Posts: 548
Joined: 27 Jun 2021, 10:46

Re: assign index when import image in imagelist

Post by SpeakEasy »

Not exactly - but you can use the before and after parameters of the AddItem method to add you item exactly where you'd like in relation to existing items

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: assign index when import image in imagelist

Post by HansV »

I don't think the ImageList control has an AddItem method.

Code: Select all

    Dim Idx As Long
   ...
   ...
        Idx = Left(NOME, InStr(NOME,"-") - 1)
        Me.ImageList1.ListImages.Add Idx, STATO, LoadPicture(PATH & NOME)
Best wishes,
Hans

User avatar
SpeakEasy
4StarLounger
Posts: 548
Joined: 27 Jun 2021, 10:46

Re: assign index when import image in imagelist

Post by SpeakEasy »

Mea culpa, you are correct

User avatar
sal21
PlatinumLounger
Posts: 4354
Joined: 26 Apr 2010, 17:36

Re: assign index when import image in imagelist

Post by sal21 »

HansV wrote:
26 Feb 2022, 10:57
I don't think the ImageList control has an AddItem method.

Code: Select all

    Dim Idx As Long
   ...
   ...
        Idx = Left(NOME, InStr(NOME,"-") - 1)
        Me.ImageList1.ListImages.Add Idx, STATO, LoadPicture(PATH & NOME)
TKS!

User avatar
SpeakEasy
4StarLounger
Posts: 548
Joined: 27 Jun 2021, 10:46

Re: assign index when import image in imagelist

Post by SpeakEasy »

This solution may not be quite what you were hoping, though, as you can never set idx higher than ImageList1.ListImages.Count + 1. So, if the first file your retrieved was 2-<whatever>.jpg it would fail. In addition, There are other gotchas.

Frankly, IMHO, you'd be better off recovering the filenames, sorting them as you want, and then using that sorted list to insert the images into the ImageList.