We help IT Professionals succeed at work.

Change forecolor of entire row in Listview VB6

Hello!
I have a question, I have a listview control in vb6 and am using the itemclick sub and I need to get the whole row selected by the user to turn red when clicked by the user.  I have been able to get the item to turn red but not the whole selected row.  any ideas?

Thanks!
Comment
Watch Question

Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Public Sub ColorListviewRow(lv As ListView, RowNbr As Long, RowColor As OLE_COLOR)
'***************************************************************************
'Purpose: Color a ListView Row
'Inputs : lv - The ListView
'         RowNbr - The index of the row to be colored
'         RowColor - The color to color it
'Outputs:
'***************************************************************************
    
    Dim itmX As ListItem
    Dim lvSI As ListSubItem
    Dim intIndex As Integer
    
    On Error GoTo ErrorRoutine
    
    Set itmX = lv.ListItems(RowNbr)
    itmX.ForeColor = RowColor
    For intIndex = 1 To lv.ColumnHeaders.Count - 1
        Set lvSI = itmX.ListSubItems(intIndex)
        lvSI.ForeColor = RowColor
    Next

    lv.ListItems(RowNbr).Selected = True
    
    Set itmX = Nothing
    Set lvSI = Nothing
    
    Exit Sub

ErrorRoutine:

    MsgBox Err.Description

End Sub

Open in new window

Author

Commented:
ok, the question now is I already have a item click routine, how do I build that into it?

I tried  call ColorListviewow(listview1, , vbred) but didnt work....
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Colorlistviewrow listview1, 5, vbRed

Should color row 5. I know the routine works because I use it in one of my apps.

Author

Commented:
I got that but, I need the row to be the row the user selected, how do I get that into the above?  O am I just stupid?  haha
"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Okay this code should work anyplace you put it after you change lvFind to the name of your listview in the several places it's used.


    Dim itmX As ListItem
    Dim lvSI As ListSubItem
    Dim intIndex As Integer
    Dim intRow As Integer
    
    For intRow = 1 To lvFind.ListItems.Count
        If lvFind.ListItems(intRow).Selected Then
            Set itmX = lvFind.ListItems(intRow)
            itmX.ForeColor = vbRed
            For intIndex = 1 To lvFind.ColumnHeaders.Count - 1
                Set lvSI = itmX.ListSubItems(intIndex)
                lvSI.ForeColor = vbRed
            Next
        End If
    Next


    Set itmX = Nothing
    Set lvSI = Nothing

Open in new window

Author

Commented:
Awesome, Thanks for the help!!
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I'm glad I was able to help.
Martin, Thank you for the support
I have an issue with my listview:- when  I click one row, the color of entire row changes but when I click another row first row remain same in color. Is there any way to return all other items to black and only the selected row to turn in red?. Thanks in advance
Biju Varkey
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Please ask a new question.