Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

ToolTipListBox - a list box with tool tips

Rate me:
Please Sign up or sign in to vote.
4.77/5 (16 votes)
31 Jul 20021 min read 184.1K   2.7K   50  
A ListBox derived class that shows tool-tips for items that won't fit within the width of the list box
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace ListBoxToolTips
{
	/// <summary>
	/// 
	/// </summary>
	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)
		{				
			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 size;
					size.cx = 0;
					size.cy = 0;
					GetTextExtentPoint32(hdc,s,s.Length,ref size);
					ReleaseDC(this.Handle,hdc);

					if(this.Width < size.cx)					
						tp.SetToolTip(this,s);

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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and 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 experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions