vb.net listview不接受颜色设置

时间:2016-05-11 15:20:11

标签: vb.net listviewitem

我不明白为什么这段代码不起作用:

    For Each rec As DataRow In rex
        Dim entry As New ListViewItem({rec("Project"), rec("effort")})
        entry.BackColor = Color.DarkSlateGray
        entry.ForeColor = Color.Ivory
        BreakdownListview.Items.Add(entry)
    Next

完全忽略2种颜色设置。该控件带有白色背景和黑色文本。

1 个答案:

答案 0 :(得分:0)

如果选择了项目,则忽略BackColor和ForeColor,最好的办法是在ListView中将OwnerDraw设置为true并自己绘制项目。

Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
    If e.Item.Selected Then
        Dim Brush As New SolidBrush(Color.SlateGray)
        e.Graphics.FillRectangle(Brush, e.Bounds)
        Brush.Dispose()
    Else
        Dim Brush As New SolidBrush(e.SubItem.BackColor)
        e.Graphics.FillRectangle(Brush, e.Bounds)
        Brush.Dispose()
    End If

    e.DrawText(TextFormatFlags.VerticalCenter)
    e.DrawFocusRectangle(e.Bounds)
End Sub

Private Sub ListView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
    e.DrawDefault = True
End Sub