Click here to Skip to main content
Licence 
First Posted 21 Oct 2003
Views 146,158
Bookmarked 60 times

ListView with Item-Hover Event

By | 21 Oct 2003 | Article
An article to display tooltips over listview items and subitems.

ListView with ItemHover event

Introduction

The ListView used by Microsoft Windows Explorer shows tool tips when a particular item is partly visible, saving the user the task of scrolling or resizing. Keeping this as my goal, I set out to achieve the same effect.

All controls that derive from System.Windows.Forms.Control have a MouseHover event. Most of the time this event is used for displaying additional or extended information about the control. For controls like the ListView, which displays different data, this event although useful, leaves the programmer aching for more information. The details about the items within the control are elusive to get at.

While there are events like ItemCheck, ItemActivate, ItemDrag, there isn't any event like the ItemHover. A little investigation with the Spy++ tool and digging through MSDN revealed that every ListView maintains an internal tool tip. This tool tip sends a message in the form of a WM_NOTIFY handler whenever it needs text from the ListView. I ended up trapping this message and exposing it off as an ItemHover event. The client can then consume this event and display additional information as needed or display a tool tip as shown in figure.

However, I quickly found out that it is very distracting after a while to see a series of tool tips flickering around on the screen. This happens because the ListView gets the message when the mouse hovers over its items, but it doesn't know whether the item or sub item is visible. It would be nice to add that information as an event argument. That was achieved by comparing the string width to the actual displayed rectangle of the item or sub item.

Armed with this information, the client can intelligently display more information about the partially hidden item, as seems fit. I tried showing it in a tool tip, you can even show it on a StatusBar or maybe the more courageous can display a Balloon Tip.

Longer lines of text wouldn't wrap around to display in the tool tip. For the purpose of the sample I wrote a small procedure to wrap those lines of text into multiple lines, by inserting the new line character. You can change this logic to fit your needs. Enjoy the code and any feedback is appreciated.

Using the code

A small example of how to use the control has been included in the form. This control works properly when the ListView is in Report mode. I haven't tested it with any other mode.

Points

The ListView can also be made to display its internal tool tip by setting its style to LVS_EX_INFOTIP. However, according to MSDN, the ListView needs to have the LVS_FULL_ROWSELECT style set too for sub items. Hence I experimented with this method.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ramshri

Web Developer

United States United States

Member

Ramesh is very much into Microsoft technologies, and silently marvels at the power of Windows SDK.
He juggles his time between his family, work and his motorcycle.
 
Find this and other code from THE source code repository at
Total .NET SourceBook

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDoes not work at all PinmemberReorX1:56 1 Aug '08  
GeneralSolution for displaying custom tooltip control on subitems [modified] Pinmembergaby_la_star5:37 21 Sep '07  
GeneralRe: Solution for displaying custom tooltip control on subitems PinmemberRussell Gantman7:11 12 Nov '07  
GeneralRe: Solution for displaying custom tooltip control on subitems PinmemberDigiOz Multimedia9:22 25 Sep '08  
GeneralRe: Solution for displaying custom tooltip control on subitems Pinmembergg670:36 8 Jan '10  
GeneralRe: Solution for displaying custom tooltip control on subitems Pinmembergaby_la_star2:10 8 Jan '10  
QuestionDisplay too slow! Pinmemberkidconan22:07 28 May '07  
QuestionSame problem in .NET 2.0 PinmemberFlyDN21:30 26 Nov '06  
QuestionI need set Tooltip for each listview header. PinmemberMichaelNu2:41 24 Oct '06  
GeneralProblems using .NET 2.0 PinmemberDanielMuhlig2:35 9 Jan '06  
GeneralSimple solution PinmemberFred Koestner3:34 26 Aug '05  
GeneralRe: Simple solution Pinmembercharlie ash16:55 30 Nov '05  
GeneralRe: Simple solution Pinmemberkidconan21:55 28 May '07  
GeneralRe: Simple solution PinmemberBaranidahran0:46 2 Aug '07  
AnswerRe: Simple solution PinmemberAndreas Saurwein Franci Gonçalves10:28 27 Oct '08  
GeneralRe: Simple solution Pinmemberfkoestner10:47 27 Oct '08  
GeneralRe: Simple solution Pinmembergeoff.appleby18:55 29 Nov '09  
GeneralLVS_EX_INFOTIP on a multi-column listview Pinmemberid10t16:20 13 Mar '05  
GeneralRe: LVS_EX_INFOTIP on a multi-column listview Pinmemberbswole3:35 14 Mar '05  
QuestionOnly displays once? Pinmemberbswole6:21 10 Mar '05  
AnswerRe: Only displays once? PinmemberSam DenHartog10:30 22 May '06  
QuestionVB.NET version ? Pinmembermistert0065:56 4 Jun '04  
AnswerRe: VB.NET version ? Pinmemberfjdlskajflsdaj;7:29 16 Jun '04  
GeneralRe: VB.NET version ? Pinmembermistert0069:34 20 Oct '04  
GeneralRe: VB.NET version ? PinsussAnonymous3:54 26 Nov '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 22 Oct 2003
Article Copyright 2003 by ramshri
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid