SHA22A
SHA22A

Reputation: 55

Keep listview item backcolor when row selected

I have a listview control with several subitems. One of the subitem columns has a background color of red, orange or green depending on the record. When the row is selected i would like the colored subitem to retain its backcolor rather than being overwritten with the selection color. Is this possible?

Upvotes: 3

Views: 1403

Answers (1)

ajakblackgoat
ajakblackgoat

Reputation: 2149

Set the ListView1.OwnerDraw to True.

Inside the ListView1's Draw... events:

Private Sub ListView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
    e.DrawDefault = True ' let System draw this element
End Sub

Private Sub ListView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
    e.DrawDefault = True ' let System draw this element
End Sub

Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
    If e.ColumnIndex = 2 Then ' only this columnindex we take over the drawing job
        e.DrawBackground() ' draw the background color
        e.DrawText()
    Else ' other subitems, let System draw them
        e.DrawDefault = True
    End If
End Sub

Upvotes: 3

Related Questions