Add images to ImageList at runtime in Visual Basic

xRuhRohx picture xRuhRohx · Jan 13, 2014 · Viewed 28.9k times · Source

I am using Visual Studio 2010 and coding in VB.

I am getting the directories in a folder to populate a ListView and have no issues doing that. I can also add images to an ImageList by searching through the folder for the files. The thing I cannot do is connect the two. If I hard code my items in and hard code my images in, they work just fine.

I also need to figure out how to add a Tag for each item. The tag should be the name of the directory. (dir from the first For Each)

Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    'Set ListView view mode to show Large Icons
    ListView1.View = View.LargeIcon

    Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
    Dim dir As String

    For Each dir In dirs
        dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
        ListView1.Items.Add(New ListViewItem(dir))
    Next

    'Create ImageList objects. 
    Dim imageListLarge As New ImageList()
    imageListLarge.ImageSize = New Size(100, 100)

    Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
    Dim files As String
    For Each files In filesList
        'files = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
        imageListLarge.Images.Add(Bitmap.FromFile(files))
    Next

    'Assign the ImageList objects to the ListView.
    ListView1.LargeImageList = imageListLarge

End Sub

Here is the same code but hard coding the info. This next example works exactly how I want it to. Only problem is Some clients might have all 6 items and some might not. Thats the reason for needing the For Each.

Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Set ListView view mode to show Large Icons
        ListView1.View = View.LargeIcon

        ' Create items for the listview
        Dim item1 As New ListViewItem("Item 1", 5)
        Dim item2 As New ListViewItem("Item 2", 4)
        Dim item3 As New ListViewItem("Item 3", 0)
        Dim item4 As New ListViewItem("Item 4", 3)
        Dim item5 As New ListViewItem("Item 5", 1)
        Dim item6 As New ListViewItem("Item 6", 2)

        'Set the group for the items
        item1.Group = ListView1.Groups("IETMs")
        item2.Group = ListView1.Groups("IETMs")
        item3.Group = ListView1.Groups("IETMs")
        item4.Group = ListView1.Groups("IETMs")
        item5.Group = ListView1.Groups("IETMs")
        item6.Group = ListView1.Groups("IETMs")

        'Set a tag for the items
        item1.Tag = "This is file 1"
        item2.Tag = "This is file 2"
        item3.Tag = "This is file 3"
        item4.Tag = "This is file 4"
        item5.Tag = "This is file 5"
        item6.Tag = "This is file 6"

        'Add the items to the ListView.
        ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3, item4, item5, item6})

        'Create ImageList objects. 
        Dim imageListLarge As New ImageList()
        imageListLarge.ImageSize = New Size(100, 100)

        ' Initialize the ImageList objects with bitmaps
        Dim image1 = Bitmap.FromFile(filePath & "Resources\IETMS\file1.ico")
        Dim image2 = Bitmap.FromFile(filePath & "Resources\IETMS\file2.ico")
        Dim image3 = Bitmap.FromFile(filePath & "Resources\IETMS\file3.ico")
        Dim image4 = Bitmap.FromFile(filePath & "Resources\IETMS\file4.ico")
        Dim image5 = Bitmap.FromFile(filePath & "Resources\IETMS\file5.ico")
        Dim image6 = Bitmap.FromFile(filePath & "Resources\IETMS\file6.ico")

        imageListLarge.Images.AddRange({image1, image2, image3, image4, image5, image6})

        'Assign the ImageList objects to the ListView.
        ListView1.LargeImageList = imageListLarge

    End Sub

Here is my modified code.

Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Set ListView view mode to show Large Icons
        ListView1.View = View.LargeIcon

        Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
        Dim dir As String

        For Each dir In dirs
            dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
            ListView1.Items.Add(New ListViewItem(dir, dir))
        Next

        'Create ImageList objects. 
        Dim imageListLarge As New ImageList()
        imageListLarge.ImageSize = New Size(100, 100)

        Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
        Dim files As String
        Dim files2 As String

        For Each files In filesList
            files2 = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
            files2 = files2.Replace(".ico", "")
            imageListLarge.Images.Add(files2, Bitmap.FromFile(files))
        Next

        'Assign the ImageList objects to the ListView.
        ListView1.LargeImageList = imageListLarge

    End Sub

Answer

LarsTech picture LarsTech · Jan 13, 2014

The ImageList collection items also can take a key for easy look-up:

imageListLarge.Images.Add(files, Bitmap.FromFile(files)))

Then you can reference them by the key:

Dim bmp As Bitmap = imageListLarge.Images("my file name")

When you add your ListView item, you can use the ImageKey as the reference:

Dim item1 As New ListViewItem("2J-F108-100", "2J-F108-100")

this assumes you added the image with the same key:

imageListLarge.Images.Add("2J-F108-100", Bitmap.FromFile(...)))

Also, make sure to add your images before adding the ListItems:

Dim imageListLarge As New ImageList()
imageListLarge.ImageSize = New Size(100, 100)
Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
Dim files2 As String
For Each files As String In filesList
  files2 = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
  files2 = files2.Replace(".ico", "")
  imageListLarge.Images.Add(files2, Bitmap.FromFile(files))
Next
ListView1.LargeImageList = imageListLarge

ListView1.View = View.LargeIcon
Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
For Each dir As String In dirs
  dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
  ListView1.Items.Add(New ListViewItem(dir, dir))
Next