Naveen's Weblog

Bridge to future

ListBox with Tooltip for each item

Posted by codingsense on January 27, 2010


Hi,

Currently I had a requirement to show tooltip for each item of the listbox control becuase, if any item lenght was exceeding the width of the listbox then it was not fully shown and i dint wanted to enable the horizontal scroolbars. So just googled for the solution and got some which would display the tooltip at the bottom of the mouse pointer itself, I found it bit annoying to the user and decided to show the tooltip starting from the right end of the listbox.

Here is the control that I created using the normal Listbox control.

Download Source Code (9.34Kb)

In this control I Overridden 3 events of the Listbox control:

  • OnMouseEnter : Here the tooltip will be Initialized
  • OnMouseLeave : Here the tooltip will be disposed
  • OnMouseMove : Here the tooltip will be shown

Here is MouseMove event,

protected override void OnMouseMove(MouseEventArgs e)
{

base.OnMouseMove(e);

//Get the index of the item on which mouse is hovering
int ListBoxItemIndex = this.IndexFromPoint(e.X, e.Y);

//If an item is present at that index then only the tooltip can be showed
if (ListBoxItemIndex > -1 && ListBoxItemIndex <= this.Items.Count)
{

//Get the string of the item
string Tip = this.Items[ListBoxItemIndex].ToString();

//If already tip of that item is shown then exit
if (Tip != CurrentTip)
{

//Get the rectangle of the item
Rectangle rect = GetItemRectangle(ListBoxItemIndex);

//Show the tooltip starting at the right border of the listbox
// and at the height of the rectangle

ItemToolTip.Show(Tip, this, this.Width, rect.Y);

//Make the tip as current tip
CurrentTip = Tip;
}
}
}


For any clarifications please revert back.

Happy Learning,
Codingsense :)

About these ads

One Response to “ListBox with Tooltip for each item”

  1. M.S.Patil said

    nice experience navin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: