Click here to Skip to main content
11,412,833 members (72,719 online)
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

Introduction

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
{
    [StructLayout(LayoutKind.Sequential)]
    public struct SIZE
    {
        public int cx;
        public int cy;
    }
    [DllImport("gdi32.dll")]
    public static extern int GetTextExtentPoint32(IntPtr hdc, 
        String str, int len, ref SIZE size);

    [DllImport("user32.dll")]
    public static extern IntPtr GetDC(IntPtr hWnd);

    [DllImport("user32.dll")]
    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;
                size.cx = 0;
                size.cy = 0;
                GetTextExtentPoint32(hdc,s,s.Length,ref size);
                ReleaseDC(this.Handle,hdc);

                /* If it won't fit show tool-tip */
                if(this.Width < size.cx)                    
                    tp.SetToolTip(this,s);

                LastIndex = index;              
            }                           
        }
    }

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

}

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

Share

About the Author

Nish Nishant

United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

Comments and Discussions

 
GeneralReally helped me out! Pin
Andrew Blackburn at 23-Dec-08 4:16
memberAndrew Blackburn23-Dec-08 4:16 
GeneralMouse Click Over Tooltip Pin
Saied Javadi at 31-Aug-08 20:05
memberSaied Javadi31-Aug-08 20:05 
QuestionTooltips for Combobox items ...???? Pin
vatsag at 16-Jan-08 20:52
membervatsag16-Jan-08 20:52 
GeneralToolTip overlay [modified] Pin
ZooZee at 1-May-07 1:39
memberZooZee1-May-07 1:39 
GeneralRe: ToolTip overlay Pin
Marco Rosas at 8-Aug-07 7:40
memberMarco Rosas8-Aug-07 7:40 
GeneralThank you Pin
rgeezy at 2-Nov-06 8:18
memberrgeezy2-Nov-06 8:18 
GeneralRe: Thank you Pin
wlin71020517 at 22-Nov-06 4:29
memberwlin7102051722-Nov-06 4:29 
GeneralRe: Thank you Pin
wlin71020517 at 22-Nov-06 4:42
memberwlin7102051722-Nov-06 4:42 
GeneralRe: Thank you Pin
Anil_Atta at 25-Apr-07 8:54
memberAnil_Atta25-Apr-07 8:54 
Questioncan this feature works in ASP??? Pin
rachellim at 9-Aug-06 18:00
memberrachellim9-Aug-06 18:00 
GeneralGreat code Pin
Scarsymmetry at 18-Jul-06 1:17
memberScarsymmetry18-Jul-06 1:17 
QuestionThis is not Supported on Framework 1.0 Pin
Tanmaysn at 29-Nov-05 1:24
memberTanmaysn29-Nov-05 1:24 
QuestionCan u do it For ASP.net Pin
.NET Follower at 6-May-05 0:42
member.NET Follower6-May-05 0:42 
AnswerRe: Can u do it For ASP.net Pin
sheijin at 5-Feb-07 20:31
membersheijin5-Feb-07 20:31 
QuestionNOT WORKING ??? Pin
De Nardis Andrea at 17-Feb-05 6:16
memberDe Nardis Andrea17-Feb-05 6:16 
AnswerRe: NOT WORKING ??? Pin
De Nardis Andrea at 17-Feb-05 6:53
memberDe Nardis Andrea17-Feb-05 6:53 
GeneralToolTip in a TreeView Control Pin
gsantanaz at 12-Jan-05 12:49
membergsantanaz12-Jan-05 12:49 
GeneralToolTip on ComboBox Pin
Deena at 27-Nov-02 20:09
memberDeena27-Nov-02 20:09 
GeneralRe: ToolTip on ComboBox Pin
Filipczako at 25-Aug-05 13:44
memberFilipczako25-Aug-05 13:44 
GeneralNice :) Pin
leppie at 1-Aug-02 8:50
memberleppie1-Aug-02 8:50 
QuestionWhat would be nice... Pin
Shog9 at 1-Aug-02 3:10
memberShog91-Aug-02 3:10 
AnswerRe: What would be nice... Pin
Nishant S at 1-Aug-02 7:05
editorNishant S1-Aug-02 7:05 
GeneralRe: What would be nice... Pin
Shog9 at 1-Aug-02 7:10
memberShog91-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
Web02 | 2.8.150427.1 | Last Updated 1 Aug 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid