Lefteris Gkinis
Lefteris Gkinis

Reputation: 1259

Icon on a Multicolumn ListView

I use this:

Public Shared Sub FulfillListView(ByVal Icon As Icon, ByVal Item1 As String, ByVal Item2 As String, ByVal sender As Object, ByVal e As System.EventArgs)
    Dim imageListSmall As New ImageList()
    vehicles_prod.OpenOrdersLB.SmallImageList = imageListSmall
    Try
        Dim SubItem As New ListViewItem("", 0)
        imageListSmall.Images.Add(Icon)
        SubItem.SubItems.Add(Item2)
        SubItem.SubItems.Add(Item1)
        vehicles_prod.OpenOrdersLB.Items.AddRange(New ListViewItem() {SubItem})


    Catch ex As Exception
        MessageBox.Show(ex.Message, "=> FulfillListView")
    End Try
End Sub

In order to Full fil a multicolumn ListView
At the start of each line (sub items lines) i want to have an icon, different each time
The only icon i see is the last entered Icon and it is the same for all the lines
Please assist me to have different icon in each line.

Upvotes: 0

Views: 2494

Answers (1)

Hans Passant
Hans Passant

Reputation: 942000

Yes, you create the image list over and over again. And every item has the same imageIndex, 0. So the items can only ever have the same icon, the last one you added. This will solve the problem:

Public Shared Sub FulfillListView(ByVal Icon As Icon, ByVal Item1 As String, ByVal Item2 As String, ByVal sender As Object, ByVal e As System.EventArgs)
    If vehicles_prod.OpenOrdersLB.SmallImageList Is Nothing Then
        vehicles_prod.OpenOrdersLB.SmallImageList = New ImageList
    End If
    vehicles_prod.OpenOrdersLB.SmallImageList.Images.Add(Icon)
    Dim SubItem As New ListViewItem("", vehicles_prod.OpenOrdersLB.SmallImageList.Images.Count - 1)
    '' etc...

But don't do this if the listview contains a lot of items. The image list will get quite large with, probably, a lot of duplicates. Which makes it slow. Explicitly managing the image list then becomes important. You can fill the image list up front with the designer, maybe that's appropriate.

Upvotes: 1

Related Questions