更改Listview子项颜色

时间:2013-11-12 12:02:22

标签: vb.net

我发现excelsheet单元格颜色如此可以在VB.net Listview中执行此操作??

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要编写ListView.DrawSubitem事件(MSDN Documentation

的代码

如果您需要绘制复杂的内容,请尝试提出更具体的问题。

enter image description here

以下是自定义绘制ListView SubItems的代码。 (创建一个表单,插入一个名为ListView1的Listview并将OwnerDraw属性设置为True)

对于依赖于价值的绘图,你需要使用你的编码技巧,幸运!!

导入System.Drawing.Drawing2D

Public Class Form2

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim itmX As ListViewItem
    For iRow As Integer = 0 To 10
        itmX = ListView1.Items.Add(iRow.ToString, iRow.ToString, "")
        For iCol As Integer = 1 To 3
            itmX.SubItems.Add(iCol.ToString)
        Next
    Next

End Sub

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

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

Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem

    Dim rnd As New Random(Now.TimeOfDay.Milliseconds)

    Dim r As Integer = rnd.Next(0, 255)
    Dim g As Integer = rnd.Next(0, 255)
    Dim b As Integer = rnd.Next(0, 255)

    Using br As New LinearGradientBrush(New Point(0, e.Bounds.Height / 2), New Point(e.Bounds.Width, e.Bounds.Height / 2), Color.FromArgb(255, r, g, b), Color.Transparent)
        e.Graphics.FillRectangle(br, e.SubItem.Bounds)
        e.Graphics.DrawRectangle(Pens.Black, e.SubItem.Bounds)
        e.DrawText()
    End Using

End Sub

结束班