
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)
{
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;
}
Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.
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 : If you are interested in hiring Nish as a consultant, you can reach him via his google email id
voidnish.
Company Website :
www.ganymedesoftwaresolutions.com