Listview image refresh problem

mskeel

Senior Contributor
Joined
Oct 30, 2003
Messages
913
I want to put a picture of a colored square in each line of a listview. Each line represents a different object and each object can have a different color. This color can be changed by the user via a color chooser dialog.

Becuase the colors change, the images in the associated imagelist for this listview cannot be static. I draw them on the fly each time the color changes. This works fine when changing the color one line at a time and the list is not so long that it is scrollable. If the list is scrollable, the images not displayed are painted the color of the image I just changed and anything I click on changes to that color.

Each line has its own image associated with it in the image list.

Some code to help explain things:

Code:
''this initially populates this list.
 Private Sub populateObjectList()
        makeSquares()

        For Each obj As DataObject In objects
            Dim item As New ListViewItem(obj.name)
            item.SubItems.Add(obj.type)
            item.SubItems.Add(obj.model)
            item.ImageIndex = 0
            objectList.Items.Add(item)
        Next
    End Sub

''This makes the squares and puts them into the imageList.
    Private Sub makeSquares()
        For i As Integer = 0 To objects.Count - 1
            Dim square As New System.Drawing.Bitmap(16, 16)

            For x As Integer = 0 To square.Height - 1
                For y As Integer = 0 To square.Width - 1
                    square.SetPixel(y, x, Color.Red)
                Next
            Next
            ImageList1.Images.Add(square)
        Next
    End Sub


''and this similiar function changes the squares.  It is called after the color chooser has returned in the action handler function.
    Private Sub makeSquare(ByVal objColor As Color, ByVal index As Integer)
        Dim square As New System.Drawing.Bitmap(16, 16)

        For x As Integer = 0 To square.Height - 1
            For y As Integer = 0 To square.Width - 1
                square.SetPixel(y, x, objColor)
            Next
        Next
        ImageList1.Images(index) = square
    End Sub

''the color Chooser function
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
        If ColorDialog1.ShowDialog() = DialogResult.OK Then
            For Each i As Integer In objectList.SelectedIndices
                objects(i).lineColor = ColorDialog1.Color
                makeSquare(ColorDialog1.Color, i)
                objectList.Items(i).ImageIndex = i
            Next

            paintCanvas()
        End If
    End Sub
my only theory is that there is some kind of strange memory issue going on when a square is reassigned.

Any thoughts? I'd appreciate any ideas or help.
 
root problem?

I knew that was going to be too much code to draw any attention. Really the only thing I care about is the root problem here. This seems like a simple enoughe problem, changing the bitmap in an imagelist and then reassigning the listiviewItem's ImageIndex. It should be a simple matter. The code seems logically sound, and in any other language, the same logic would be sound.

What I want to know is how the pointer system in Visual Basic .net works. What am I actually pointing to when I say objectList.Items(i).ImageIndex = i ? It doesn't seem actually do anything? What happens when I reuse a variable like this?
Code:
For i As Integer = 0 To objects.Count - 1
            Dim square As New System.Drawing.Bitmap(16, 16)
''    do some stuff to it
            ImageList1.Images.Add(square)
        Next
That's the bottom line. If this is a goofy, memory/pointer issue, I want to know what it is and why it happens. Am I assigning a pointer to an array or am I assigning the thing the pointer points to? Because, let's face it, vb really doesn't make this clear at all and next to better languages like C++, VB really lacks.
 
Back
Top