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:
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.
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
Any thoughts? I'd appreciate any ideas or help.