Steve Long said:
I hope this isn't too stupid of a question but I'm looking for a way to
change an item in a listview control when the mouse moves over it. I'd like
to change its color and underline it for a probable internet link. Is there
a way to do that? I've looked through the help and can't find anything that,
well, helps. Perhaps I'm using the wrong control.
Any help would be appreciated.
A quick 'n' dirty method using a ListView with hovering underlines on the label only.
Note how the RefreshItem routine ensures "flickering" is kept to a minimum.
Micky
Private m_pItem As ListViewItem
Private m_iMouseX As Integer
Private m_iMouseY As Integer
Private m_pFontUnderline As Font
Private m_pFontRegular As Font
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_pItem = Nothing
m_iMouseX = -1
m_iMouseY = -1
m_pFontRegular = New Font(m_pListView.Font, FontStyle.Regular)
m_pFontUnderline = New Font(m_pFontRegular, FontStyle.Underline)
m_pListView.View = View.Details
With m_pListView.Columns
..Add("Column1", 100, HorizontalAlignment.Left)
..Add("Column2", 100, HorizontalAlignment.Left)
..Add("Column3", 100, HorizontalAlignment.Left)
End With
With m_pListView.Items.Add("Item1")
..SubItems.Add("Sub1")
..SubItems.Add("Sub2")
..UseItemStyleForSubItems = False
End With
With m_pListView.Items.Add("Item2")
..SubItems.Add("Sub1")
..SubItems.Add("Sub2")
..UseItemStyleForSubItems = False
End With
With m_pListView.Items.Add("Item3")
..SubItems.Add("Sub1")
..SubItems.Add("Sub2")
..UseItemStyleForSubItems = False
End With
End Sub
Private Sub m_pListView_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
m_pListView.MouseLeave
m_iMouseX = -1
m_iMouseY = -1
RefreshItem()
End Sub
Private Sub m_pListView_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles
m_pListView.MouseMove
m_iMouseX = e.X
m_iMouseY = e.Y
RefreshItem()
End Sub
Private Sub RefreshItem()
Dim pItem As ListViewItem = m_pListView.GetItemAt(m_iMouseX, m_iMouseY)
If Not pItem Is m_pItem Then
m_pListView.BeginUpdate()
If Not m_pItem Is Nothing Then
m_pItem.SubItems.Item(0).Font = m_pFontRegular
End If
m_pItem = pItem
If Not m_pItem Is Nothing Then
m_pItem.SubItems.Item(0).Font = m_pFontUnderline
End If
m_pListView.EndUpdate()
End If
End Sub