Search code examples
vb.netimagelistobjectlistview

How to use ObjectListView to show all the images in a imagelist?


How do I use a ObjectListView to show all the images in a image-list ? In the home-site of ObjectListView they have shown how to do this in C# (I am not that good with c# and delegates). But I can't get it to work in VB.net.


Solution

  • Here is a direct translation of their example:

    Me.mainColumn.ImageGetter = Function(row As Object) Do
        Dim key As String = Me.GetImageKey(row)
        If Not Me.listView.LargeImageList.Images.ContainsKey(key) Then
            Dim smallImage As Image = Me.GetSmallImageFromStorage(key)
            Dim largeImage As Image = Me.GetLargeImageFromStorage(key)
            Me.listView.SmallImageList.Images.Add(key, smallImage)
            Me.listView.LargeImageList.Images.Add(key, largeImage)
        End If
        Return key
    End Function
    

    That will only work with the most recent version of VB.NET because it uses an inline function. You can alter it like this for older versions:

    Create a function similar to:

    Public Function GetImageFromList(row As Object) As String
            Dim key As String = Me.GetImageKey(row)
            If Not Me.listView.LargeImageList.Images.ContainsKey(key) Then
                Dim smallImage As Image = Me.GetSmallImageFromStorage(key)
                Dim largeImage As Image = Me.GetLargeImageFromStorage(key)
                Me.listView.SmallImageList.Images.Add(key, smallImage)
                Me.listView.LargeImageList.Images.Add(key, largeImage)
            End If
            Return key
    End Function
    

    And then set your image getter for the column to it like:

    Me.mainColumn.ImageGetter = AddressOf GetImageFromList