ListView OwnerDrawn Cell

Rossmc

Member
Joined
Mar 17, 2009
Messages
12
Programming Experience
10+
Hi

I'm using a ListView as a chart legend. Each row in listview represents a series on the chart, with subitems representing various elements of data represented by series.

In the first cell of each row I want a checkbox, a rectangle filled with the colour of the series and then some text which is the name of the series.

Any ideas/help? Greatly appreciated.
 
Resolved

Found a fairly simple quick fix for this, without resorting to an ownerdrawn listview.

1) Added ListView to form. View=Details, Checkboxes=True
2) Added an EMPTY ImageList control to the form and assigned it to the SmallImageList property of the ListView control.
3) Just prior to loading each series' info into the Items collection, I first determine the colour of the series, dynamically draw a rectangle of the same colour and add it to the ImageList, Images collection using the color.tostring as the Image key.
4) When I then add the series I assign it the image with the key that is the same as the series colour.tostring.

Simple!

Here is some code:
To add the image to the ImageList:
Dim c as new system.drawing.color = series.color
Dim t As Image = ilSeries.Images(c.ToString)

'Only add images that are not already available
If t Is Nothing Then
Dim o As New Bitmap(24, 16)
Dim d As Graphics = Graphics.FromImage(o)

d.FillRectangle(New SolidBrush(c), 1, 4, 22, 10) 'Colour rectangle
d.DrawLine(New Pen(Color.Black, 1), 1, 3, 22, 3) 'Top
d.DrawLine(New Pen(Color.Black, 1), 1, 13, 22, 13) 'Bottom
d.DrawLine(New Pen(Color.Black, 1), 1, 3, 1, 13) 'Left
d.DrawLine(New Pen(Color.Black, 1), 22, 3, 22, 13) 'Right

'Add the new image to the image list
ilSeries.Images.Add(c.ToString, o)
End If
 
Back
Top