Results 1 to 5 of 5

Thread: [RESOLVED] Hover triggers a change of ForeColor of one ListView row

  1. #1

    Thread Starter
    Junior Member DTML's Avatar
    Join Date
    Mar 2006
    Location
    Duluth, MN, USA
    Posts
    19

    Resolved [RESOLVED] Hover triggers a change of ForeColor of one ListView row

    When "hovering" (MouseMove) over a row in a listview, I would like to temporarily change the ForeColor property of that one row.

    There seems to be an error in the code offered in this post by "anotherVBnewbie":
    http://www.vbforums.com/showthread.p...ricky+listview

    (I get a compiler error, stating that LVM_HITTEST is not defined)

    ...but I think it is on the track towards what I want to do.

    Rather than:

    VB Code:
    1. If (lItemIndex >= 1) And (lItemIndex <= listview.ListItems.Count) Then
    2.    listview.ToolTipText = "whatever you want to tip to be"
    3. Else
    4.    listview.ToolTipText = ""
    5. End If
    I would probably need something like:
    VB Code:
    1. If (lItemIndex >= 1) And (lItemIndex <= listview.ListItems.Count) Then
    2.    listview.ListItems(lItemIndex).ForeColor = &H8080FF
    3. Else
    4.    listview.ListItems(lItemIndex).ForeColor = &HFFFFC0
    5. End If

    Anybody got an easy solution to this?

    TIA,

    Dennis
    Last edited by DTML; Mar 14th, 2006 at 04:18 PM.

  2. #2

  3. #3

    Thread Starter
    Junior Member DTML's Avatar
    Join Date
    Mar 2006
    Location
    Duluth, MN, USA
    Posts
    19

    Re: Hover triggers a change of ForeColor of one ListView row

    Marty, Thanks for the reply!

    I had already added this:

    Const LVM_FIRST = &H1000&
    Const LVM_HITTEST = LVM_FIRST + 18

    but changed it to your declaration.

    I think I am close (I can have the ToolTip include/display the index number of the item I am over), so I guess I either do not know the syntax for the row's ForeColor, or the ListView just won't do it. I have tried:

    VB Code:
    1. lvwTOC.ListItems(lItemIndex).ForeColor = &H8080FF
    and
    VB Code:
    1. lvwTOC.ListItems.Item(lItemIndex).ForeColor = &H8080FF

    I'm pretty sure it's not the SelectedItem, because there has been no click event, but sometimes I am surprised by "features" when I try to use properties and methods of controls that are new to me.

    [scratching head] [/scratching head]

    I also get an out of bounds error if I move the mouse down past the last listitem entry. (the lItemIndex becomes zero), so even though I am testing for the range of that index, I guess the call to SendMessage is changing the index after the test. For that, I wonder if re-testing for the range before referencing it within the sub that already tested (and passed) it will even be fast enough to trap that error?

    [scratching head] [/scratching head]

    Dennis

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Hover triggers a change of ForeColor of one ListView row

    I looked up an old program of mine and here is what I do.

    VB Code:
    1. Public Sub ColorListviewRow(lv As ListView, RowNbr As Long, RowColor As OLE_COLOR)
    2. '***************************************************************************
    3. 'Purpose: Color a ListView Row
    4. 'Inputs : lv - The ListView
    5. '         RowNbr - The index of the row to be colored
    6. '         RowColor - The color to color it
    7. 'Outputs:
    8. '***************************************************************************
    9.    
    10.     Dim itmX As ListItem
    11.     Dim lvSI As ListSubItem
    12.     Dim intIndex As Integer
    13.    
    14.     On Error GoTo ErrorRoutine
    15.    
    16.     Set itmX = lv.ListItems(RowNbr)
    17.     itmX.ForeColor = RowColor
    18.     For intIndex = 1 To lv.ColumnHeaders.Count - 1
    19.         Set lvSI = itmX.ListSubItems(intIndex)
    20.         lvSI.ForeColor = RowColor
    21.     Next
    22.  
    23.     Set itmX = Nothing
    24.     Set lvSI = Nothing
    25.    
    26.     Exit Sub
    27.  
    28. ErrorRoutine:
    29.  
    30.     MsgBox Err.Description
    31.  
    32. End Sub

    Usage:
    VB Code:
    1. ColorListviewRow MyListView, 5, vbRed ' use 16711681 instead of vbBlue

  5. #5

    Thread Starter
    Junior Member DTML's Avatar
    Join Date
    Mar 2006
    Location
    Duluth, MN, USA
    Posts
    19

    Re: Hover triggers a change of ForeColor of one ListView row

    (long interruption for family stuff...)

    Marty, You're right that I needed to address the SubItems in the row, not just the row.

    Using a combination of your code and the MouseMove with SendMessage, I was able to get it to work. One other thing that was necessary (with the way this is coded) was to set the HoverSelection property to true. I need to gain a better understanding of ListView controls, because right now it seems as if there were 3 possible states for Selected: "Not Selected", "Pre-Selected", and "Selected." That intermediate state of "pre-selected" seemed to be what happened if either I scrolled through the listview with arrow keys, or by moving the mouse over a listview row with HoverSelection = True. If HoverSelection is set to False, then the rows did not appear to change color unless I scrolled over the rows with arrow keys. I guess another possibility is that the code works, but lacks a "Refresh" to show the new row color. And, it may be that HoverSelection as well as moving over each listview row with the arrow keys' KeyPress event does an automatic Refresh to either the row or to the entire listview.

    Anyway, though I don't completely understand why HoverSelection makes it work, it not only works, it uses the pointy-finger cursor automatically (which works well for this effect), and then gives the user strong feedback on the specific row where they stopped the mouse by highlighting (and selecting) the row.

    I set a module-level variable to keep track of the previous row that had been under the cursor, so I could change its color back. (Colors in this example are hard-coded, but they could easily be variables.)

    So, with Marty's code, and anotherVBnewbie's code, combined, modified, sliced and diced, here is some sample code to display a listview that changes the color of the row under the cursor. Hope this comes in handy for someone else.

    Thank you Marty, and anotherVBnewbie (for your archived code) I sure do appreciate the assistance!

    Dennis

    VB Code:
    1. Option Explicit
    2.  
    3. Private m_lngPreviousRow As Long
    4.  
    5. Private Const LVM_FIRST As Long = &H1000
    6. Private Const LVM_HITTEST As Long = (LVM_FIRST + 18)
    7.  
    8. Private Type POINTAPI
    9.     x As Long
    10.     Y As Long
    11. End Type
    12.  
    13. Private Type LVHITTESTINFO
    14.    pt As POINTAPI
    15.    flags As Long
    16.    iItem As Long
    17.    iSubItem As Long
    18. End Type
    19.  
    20. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    21.     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wparam As Long, _
    22.     lParam As Any) As Long
    23.  
    24.  
    25. 'this public sub would probably be in a .bas module
    26. Public Sub ColorListviewRow(lv As ListView, RowNbr As Long, RowColor As OLE_COLOR)
    27. '***************************************************************************
    28. 'Purpose: Color a ListView Row
    29. 'Inputs : lv - The ListView
    30. '         RowNbr - The index of the row to be colored
    31. '         RowColor - The color to color it
    32. 'Outputs:
    33. '***************************************************************************
    34.    
    35.     Dim itmX As ListItem
    36.     Dim lvSI As ListSubItem
    37.     Dim intIndex As Integer
    38.    
    39.     On Error GoTo ErrorRoutine
    40.    
    41.     Set itmX = lv.ListItems(RowNbr)
    42.     itmX.ForeColor = RowColor
    43.     For intIndex = 1 To lv.ColumnHeaders.Count - 1
    44.         Set lvSI = itmX.ListSubItems(intIndex)
    45.         lvSI.ForeColor = RowColor
    46.     Next
    47.  
    48.     Set itmX = Nothing
    49.     Set lvSI = Nothing
    50.    
    51.     Exit Sub
    52.  
    53. ErrorRoutine:
    54.  
    55.     MsgBox Err.Description
    56.  
    57. End Sub
    58.  
    59. Private Sub lvwTOC_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
    60.     Dim lvhti As LVHITTESTINFO
    61.     Dim lngCurrentRow As Long
    62.    
    63.     lvhti.pt.x = x / Screen.TwipsPerPixelX
    64.     lvhti.pt.Y = Y / Screen.TwipsPerPixelY
    65.     lngCurrentRow = SendMessage(lvwTOC.hwnd, LVM_HITTEST, 0, lvhti) + 1
    66.        
    67.     'check to see if the MouseMove has changed which row is under the mouse:
    68.     If lngCurrentRow <> m_lngPreviousRow Then 'row has changed
    69.         If ((m_lngPreviousRow > 0) And (m_lngPreviousRow <= lvwTOC.ListItems.Count)) Then
    70.             'so, we need to reset the old row's ForeColor:
    71.             ColorListviewRow lvwTOC, m_lngPreviousRow, &HFFFFC0 'light blue
    72.             If ((lngCurrentRow > 0) And (lngCurrentRow <= lvwTOC.ListItems.Count)) Then
    73.                 'and set the current row's ForeColor:
    74.                 ColorListviewRow lvwTOC, lngCurrentRow, &H8080FF 'light red
    75.             End If
    76.         Else 'first time through (m_lngPreviousRow = -1 in FormLoad)
    77.             If ((lngCurrentRow > 0) And (lngCurrentRow <= lvwTOC.ListItems.Count)) Then
    78.                 ColorListviewRow lvwTOC, lngCurrentRow, &H8080FF 'light red
    79.             End If
    80.         End If
    81.  
    82.         'and update he module level variable to indicate that the current row is now the old row:
    83.         m_lngPreviousRow = lngCurrentRow
    84.     End If
    85.    
    86.    End Sub
    Last edited by DTML; Mar 14th, 2006 at 11:08 PM.

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