如何在Listview中制作颜色

时间:2012-01-15 06:53:02

标签: listview vb6

如何在子项目中制作颜色......

列表视图

Listview1.Item(1).SubItems(2) 'I want ot make color on this column

如何做到这一点。

1 个答案:

答案 0 :(得分:0)

以下代码将执行您所需的操作(intCol是基于零的列,您需要更改颜色):

Public Sub SetListviewItemColour(lvControl As ListView, intRow As Long, ByVal intCol As Integer, lngColour As Long)

    Dim liItem As ListItem
    Dim liSubItem As ListSubItem
    Dim intIndex As Integer

    On Error GoTo errHand

    Set liItem = lvControl.ListItems(intRow)
    If intCol = 0 Then
        liItem.ForeColor = lngColour
        GoTo CleanUp
    End If

    For intIndex = 1 To lvControl.ColumnHeaders.Count - 1
    If intIndex = intCol Then
        Set liSubItem = liItem.ListSubItems(intIndex)
        liSubItem.ForeColor = lngColour
        GoTo CleanUp
        End If
    Next

CleanUp:
    Set liItem = Nothing
    Set liSubItem = Nothing

    Exit Sub
errHand:
    MsgBox Err.Description
End Sub

用法:

Call SetListviewItemColour(ListView1, 3, 1, vbRed)

注意我使用code from VBForums作为此代码段的基础并将其扩展为使用列号