By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

Change Item in listview on mouse over

P: n/a
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.

Thanks
Steve
Nov 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Steve Long" <St**********@NoSpam.com> wrote in message news:ur**************@TK2MSFTNGP09.phx.gbl...
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
Nov 23 '05 #2

P: n/a
That's almost exactly what I was looking for Micky. With a slight
modification to the RefreshItem routine, it does exactly what I want. See
below:

The only problem that I can see is that of when the mouse point is turning
into a hand. It's very finicky

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
m_pItem.SubItems.Item(0).ForeColor = Color.Black
m_pListView.Cursor.Current = Cursors.Default
End If
m_pItem = pItem
If Not m_pItem Is Nothing Then
m_pItem.SubItems.Item(0).Font = m_pFontUnderline
m_pItem.SubItems.Item(0).ForeColor = Color.Blue
m_pListView.Cursor.Current = Cursors.Hand
End If
m_pListView.EndUpdate()
End If
End Sub
"Micky" <mi***@n05pam.com> wrote in message
news:dl**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
"Steve Long" <St**********@NoSpam.com> wrote in message news:ur**************@TK2MSFTNGP09.phx.gbl...
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

Nov 23 '05 #3

P: n/a
You simply need to check whether m_pItem actually points to
an item or not, and then switch the cursor accordingly. You must
also continually update the cursor each time the mouse moves,
whether the item has changed or not.

Here's the solution, with comments. Note the use of the With
statement to keep dereferencing to a minimum. It also makes
the code easier to read (and write):

Private Sub RefreshItem()
' point to current item under cursor
Dim pItem As ListViewItem = m_pListView.GetItemAt(m_iMouseX, m_iMouseY)
' compare with previous item (from last time around)
If Not pItem Is m_pItem Then
' item has changed
m_pListView.BeginUpdate()
' restore previous item, if any
If Not m_pItem Is Nothing Then
With m_pItem.SubItems.Item(0)
.Font = m_pFontRegular
.ForeColor = Color.Black
End With
End If
' now point to current item (may be nothing)
m_pItem = pItem
' update current item, if any
If Not m_pItem Is Nothing Then
With m_pItem.SubItems.Item(0)
.Font = m_pFontUnderline
.ForeColor = Color.Blue
End With
End If
m_pListView.EndUpdate()
End If
' now update the cursor
If m_pItem Is Nothing Then
m_pListView.Cursor.Current = Cursors.Default
Else
m_pListView.Cursor.Current = Cursors.Hand
End If
End Sub

"Steve Long" <St**********@NoSpam.com> wrote in message news:OB**************@TK2MSFTNGP09.phx.gbl...
That's almost exactly what I was looking for Micky. With a slight
modification to the RefreshItem routine, it does exactly what I want. See
below:

The only problem that I can see is that of when the mouse point is turning
into a hand. It's very finicky

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
m_pItem.SubItems.Item(0).ForeColor = Color.Black
m_pListView.Cursor.Current = Cursors.Default
End If
m_pItem = pItem
If Not m_pItem Is Nothing Then
m_pItem.SubItems.Item(0).Font = m_pFontUnderline
m_pItem.SubItems.Item(0).ForeColor = Color.Blue
m_pListView.Cursor.Current = Cursors.Hand
End If
m_pListView.EndUpdate()
End If
End Sub
"Micky" <mi***@n05pam.com> wrote in message
news:dl**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
"Steve Long" <St**********@NoSpam.com> wrote in message

news:ur**************@TK2MSFTNGP09.phx.gbl...
>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


Nov 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.