Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ListView with Item-Hover Event

0.00/5 (No votes)
21 Oct 2003 1  
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