I was working on a project where I needed to have a list-box that would show
tool tips for items that will not fit within the width of the list-box.
Initially I thought there would be a .NET BCL class that would have this
facility. I've had forgettable experiences in the past where I'd waste my time
writing something that was already available. But this time I found nothing that
met my requirements. So I wrote my own list box class derived from the .NET
System.Windows.Forms.ListBox class and called it
What it does
Whenever an item in the
ToolTipListBox list-box exceeds the width of the list-box
control, a tool-tip is floated just over the item. Tool-tips are floated only
for items that won't fit within the width of the list-box. For items that do fit
in, the tool-tip will not be shown.
Simply declare your list-box objects as members of the
ToolTipListBox class instead of the standard ListBox class.
That's all. You'll also have to include the
ToolTipListBox class source file in your project. Or you
might even build a library and reference it instead.
Class source listing
public class ToolTipListBox : System.Windows.Forms.ListBox
public struct SIZE
public int cx;
public int cy;
public static extern int GetTextExtentPoint32(IntPtr hdc,
String str, int len, ref SIZE size);
public static extern IntPtr GetDC(IntPtr hWnd);
public static extern int ReleaseDC(IntPtr hWnd,IntPtr hdc);
tp.InitialDelay = 500;
tp.ReshowDelay = 500;
tp.AutoPopDelay = 3000;
tp.Active = true;
protected override void OnMouseMove(
int index = IndexFromPoint(e.X,e.Y);
if(index != ListBox.NoMatches )
if( LastIndex != index )
string s = Items[index].ToString();
IntPtr hdc = GetDC(this.Handle);
size.cx = 0;
size.cy = 0;
if(this.Width < size.cx)
LastIndex = index;
private ToolTip tp = new ToolTip();
private int LastIndex = -1;
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com
where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket
as well as a programming book – Extending MFC applications with the .NET Framework
Nish's latest book C++/CLI in Action
published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.