Results 1 to 4 of 4

Thread: VB - Change color of font in ListView Row when highlighted

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014

    VB - Change color of font in ListView Row when highlighted

    Hi, in the below script I have changed the font of the ListView row based on an expiration date. How can I keep the color of the font when the row is highlighted? For example, if my font is red for a specific row it turns to white when that row is selected.

    Thanks for any help!

    'Loop through records and see if colors need to be changed due to expiration date
    For Counter = 1 To lstUser.ListItems.Count
    Set Item = lstUser.ListItems.Item(Counter)

    If CDate(Item.SubItems(11)) <= Date Then
    If Item.Selected Then
    lstUser.SelectedItem.ForeColor = vbRed
    End If
    Item.ForeColor = vbRed
    Item.ListSubItems(1).ForeColor = vbRed
    Item.ListSubItems(2).ForeColor = vbRed
    Item.ListSubItems(3).ForeColor = vbRed
    Item.ListSubItems(4).ForeColor = vbRed
    Item.ListSubItems(5).ForeColor = vbRed
    Item.ListSubItems(6).ForeColor = vbRed
    Item.ListSubItems(7).ForeColor = vbRed
    Item.ListSubItems(8).ForeColor = vbRed
    Item.ListSubItems(9).ForeColor = vbRed
    Item.ListSubItems(10).ForeColor = vbRed
    Item.ListSubItems(11).ForeColor = vbRed
    Item.ListSubItems(12).ForeColor = vbRed
    ElseIf CDate(Item.SubItems(11)) < Date + 30 Then
    lstUser.Item(Counter).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(1).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(2).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(3).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(4).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(5).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(6).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(7).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(8).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(9).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(10).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(11).ForeColor = RGB(255, 153, 0)
    lstUser.Item(Counter).ListSubItems(12).ForeColor = RGB(255, 153, 0)
    End If

    Next Counter

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    An obscure body in the SK system. The inhabitants call it Earth

    Re: VB - Change color of font in ListView Row when highlighted

    Thread moved. Code Bank is for Code Submissions rather than questions.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    Join Date
    Feb 2006

    Re: VB - Change color of font in ListView Row when highlighted

    VB6 code is not script.

    To answer your question:

    Short answer, you can't.

    Longer answer, you may have to use subclassing to capture selection and then owner-draw what you want.

  4. #4
    Frenzied Member
    Join Date
    May 2006
    some place in the cloud

    Re: VB - Change color of font in ListView Row when highlighted

    I do it next way to change forecolor of listview cells
    Dim LI   As MSComctlLib.ListItem
    Dim LIS  As MSComctlLib.ListSubItem
    'Add to ListView1  -1 row  -2 columns  -forecolor blue
        Set LI = ListView1.ListItems.Add(, , "TestCol 0")
            LI.ForeColor = vbBlue
            LI.SubItems(1) = "TestCol 1"
            Set LIS = LI.ListSubItems(1)
                LIS.ForeColor = vbBlue
            Set LIS = Nothing

    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width