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
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.
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.