|
-
Mar 14th, 2006, 04:10 PM
#1
Thread Starter
Junior Member
[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:
If (lItemIndex >= 1) And (lItemIndex <= listview.ListItems.Count) Then
listview.ToolTipText = "whatever you want to tip to be"
Else
listview.ToolTipText = ""
End If
I would probably need something like:
VB Code:
If (lItemIndex >= 1) And (lItemIndex <= listview.ListItems.Count) Then
listview.ListItems(lItemIndex).ForeColor = &H8080FF
Else
listview.ListItems(lItemIndex).ForeColor = &HFFFFC0
End If
Anybody got an easy solution to this?
TIA,
Dennis
Last edited by DTML; Mar 14th, 2006 at 04:18 PM.
-
Mar 14th, 2006, 04:21 PM
#2
Re: Hover triggers a change of ForeColor of one ListView row
Add these two declarations
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_HITTEST As Long = (LVM_FIRST + 18)
-
Mar 14th, 2006, 05:03 PM
#3
Thread Starter
Junior Member
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:
lvwTOC.ListItems(lItemIndex).ForeColor = &H8080FF
and
VB Code:
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
-
Mar 14th, 2006, 05:13 PM
#4
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:
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
Set itmX = Nothing
Set lvSI = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Description
End Sub
Usage:
VB Code:
ColorListviewRow MyListView, 5, vbRed ' use 16711681 instead of vbBlue
-
Mar 14th, 2006, 10:57 PM
#5
Thread Starter
Junior Member
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:
Option Explicit
Private m_lngPreviousRow As Long
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_HITTEST As Long = (LVM_FIRST + 18)
Private Type POINTAPI
x As Long
Y As Long
End Type
Private Type LVHITTESTINFO
pt As POINTAPI
flags As Long
iItem As Long
iSubItem As Long
End Type
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wparam As Long, _
lParam As Any) As Long
'this public sub would probably be in a .bas module
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
Set itmX = Nothing
Set lvSI = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Description
End Sub
Private Sub lvwTOC_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
Dim lvhti As LVHITTESTINFO
Dim lngCurrentRow As Long
lvhti.pt.x = x / Screen.TwipsPerPixelX
lvhti.pt.Y = Y / Screen.TwipsPerPixelY
lngCurrentRow = SendMessage(lvwTOC.hwnd, LVM_HITTEST, 0, lvhti) + 1
'check to see if the MouseMove has changed which row is under the mouse:
If lngCurrentRow <> m_lngPreviousRow Then 'row has changed
If ((m_lngPreviousRow > 0) And (m_lngPreviousRow <= lvwTOC.ListItems.Count)) Then
'so, we need to reset the old row's ForeColor:
ColorListviewRow lvwTOC, m_lngPreviousRow, &HFFFFC0 'light blue
If ((lngCurrentRow > 0) And (lngCurrentRow <= lvwTOC.ListItems.Count)) Then
'and set the current row's ForeColor:
ColorListviewRow lvwTOC, lngCurrentRow, &H8080FF 'light red
End If
Else 'first time through (m_lngPreviousRow = -1 in FormLoad)
If ((lngCurrentRow > 0) And (lngCurrentRow <= lvwTOC.ListItems.Count)) Then
ColorListviewRow lvwTOC, lngCurrentRow, &H8080FF 'light red
End If
End If
'and update he module level variable to indicate that the current row is now the old row:
m_lngPreviousRow = lngCurrentRow
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|