选中行时,保持listview项目背景色

时间:2013-07-09 10:08:50

标签: vb.net

我有一个包含几个子项的listview控件。其中一个子项目列的背景颜色为红色,橙色或绿色,具体取决于记录。当选择行时,我希望彩色子项保留其背景颜色而不是被选择颜色覆盖。这可能吗?

1 个答案:

答案 0 :(得分:2)

ListView1.OwnerDraw设置为True

在ListView1的 Draw ... 事件中:

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