473,378 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Change Item in listview on mouse over

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
3 4842
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RWD | last post by:
I am trying to figure out how to change the target frame in my hyperlink on a DHTML menu. The menu is in one frame and the target frame is called "main" The code is below: Thanks in advance...
13
by: Maheshkumar.R | last post by:
hi groups, I have placed an listview control, i want to iterate thru the control and find the clicked event items. listView2.Items.Add(fname.ToString(), i); how i can perform the iteration...
2
by: Dean Slindee | last post by:
Is it possible to determine which listview item a mouse is hovering over? Thanks, Dean Slindee
4
by: Pucca | last post by:
How can I tell a mouse right clicks over a listview item that's in a container panel. I only want to display a popup menu if the user right click the mouse over an item on the Listview. I don't...
2
by: shapper | last post by:
Hello, How can I change the background color of an Asp:Panel when the mouse is over it? Thanks, Miguel
3
by: rn5a | last post by:
I want to change the background color of a row in a DataList when the mouse is moved over a row. This is how I tried but it doesn't change the background color of a row in the DataList when the...
1
by: apondu | last post by:
Hi, This is Govadhan, i am a begginer for web services using c#.net. I have written a small web service where in i have created a listview item in the web service method and added some data to...
5
by: Jure Bogataj | last post by:
Hi all! I have a problem (performance issue) with listview. I have implemented an ItemSelectionChange on my listview and put some code in it (I build some toolbar based on selection and update...
4
by: Brandon | last post by:
HI all, I am working on a WPF listview that get items through the XMLDataProvider and making a listview with two actual columns and in both CellTemplete XAML in a grid bind more than one snippet...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.