Click here to Skip to main content
Click here to Skip to main content

ToolTipListBox - a list box with tool tips

, 31 Jul 2002
Rate this:
Please Sign up or sign in to vote.
A ListBox derived class that shows tool-tips for items that won't fit within the width of the list box


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

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.

Using it

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

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

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);

    public ToolTipListBox()
        tp.InitialDelay = 500;
        tp.ReshowDelay = 500;
        tp.AutoPopDelay = 3000;         
        tp.Active = true;           

    protected override void OnMouseMove(
        System.Windows.Forms.MouseEventArgs e)
        /* Get the index of the mouse-hovered item */
        int index = IndexFromPoint(e.X,e.Y);

        /* Ensure that there is an item */
        if(index != ListBox.NoMatches )
              Check if the mouse has moved enough
              distance for a new index 
            if( LastIndex != index )
                string s = Items[index].ToString(); 

                /* Get the text extent */
                IntPtr hdc = GetDC(this.Handle);
                SIZE size;
       = 0;
       = 0;
                GetTextExtentPoint32(hdc,s,s.Length,ref size);

                /* If it won't fit show tool-tip */
                if(this.Width <                    

                LastIndex = index;              

    private ToolTip tp = new ToolTip();
    private int LastIndex = -1;



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

Nish Nishant

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

Comments and Discussions

GeneralReally helped me out! PinmemberAndrew Blackburn23-Dec-08 4:16 
GeneralMouse Click Over Tooltip PinmemberSaied Javadi31-Aug-08 20:05 
QuestionTooltips for Combobox items ...???? Pinmembervatsag16-Jan-08 20:52 
GeneralToolTip overlay [modified] PinmemberZooZee1-May-07 1:39 
I've chaged your code a little bit so that the ToolTip is displayed over the items in the ListBox.
There is no PInvoke here.
Here we go:
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
// Get the index of the mouse-hovered item
int index = base.IndexFromPoint(e.X, e.Y);
// Ensure that there is an item
if (index != ListBox.NoMatches)
// Check if the mouse has moved enough distance for a new index
if (LastIndex != index)
string s = Items[index].ToString();
Size textSize = TextRenderer.MeasureText(s, base.Font);
Rectangle r = base.GetItemRectangle(index);
if (r.Width < textSize.Width)
// Display the TT over the LB item
tp.Show(s, this, r.X+1, r.Y+2);
LastIndex = index;
// I use this event handler because I use a different
// font in the LB, so a matching TT font is needed
void tp_Draw(object sender, DrawToolTipEventArgs e)
//Draw the standard background
// Draw the standard border
// Draw the custom text
TextRenderer.DrawText(e.Graphics, e.ToolTipText, base.Font, e.Bounds, Color.Black, TextFormatFlags.HorizontalCenter);
// Changes the TT's size
void tp_Popup(object sender, PopupEventArgs e)
Size s = TextRenderer.MeasureText(tp.GetToolTip(this), base.Font);
s.Height += 3;
e.ToolTipSize = s;

Thank you Nishant for posting this article.


-- modified at 11:32 Wednesday 2nd May, 2007
GeneralRe: ToolTip overlay PinmemberMarco Rosas8-Aug-07 7:40 
GeneralThank you Pinmemberrgeezy2-Nov-06 8:18 
GeneralRe: Thank you Pinmemberwlin7102051722-Nov-06 4:29 
GeneralRe: Thank you Pinmemberwlin7102051722-Nov-06 4:42 
GeneralRe: Thank you PinmemberAnil_Atta25-Apr-07 8:54 
Questioncan this feature works in ASP??? Pinmemberrachellim9-Aug-06 18:00 
GeneralGreat code PinmemberScarsymmetry18-Jul-06 1:17 
QuestionThis is not Supported on Framework 1.0 PinmemberTanmaysn29-Nov-05 1:24 
QuestionCan u do it For Pinmember.NET Follower6-May-05 0:42 
AnswerRe: Can u do it For Pinmembersheijin5-Feb-07 20:31 
QuestionNOT WORKING ??? PinmemberDe Nardis Andrea17-Feb-05 6:16 
AnswerRe: NOT WORKING ??? PinmemberDe Nardis Andrea17-Feb-05 6:53 
GeneralToolTip in a TreeView Control Pinmembergsantanaz12-Jan-05 12:49 
GeneralToolTip on ComboBox PinmemberDeena27-Nov-02 20:09 
GeneralRe: ToolTip on ComboBox PinmemberFilipczako25-Aug-05 13:44 
GeneralNice :) Pinmemberleppie1-Aug-02 8:50 
QuestionWhat would be nice... PinmemberShog91-Aug-02 3:10 
AnswerRe: What would be nice... PineditorNishant S1-Aug-02 7:05 
GeneralRe: What would be nice... PinmemberShog91-Aug-02 7:10 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 1 Aug 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid