Click here to Skip to main content
11,647,810 members (69,802 online)
Click here to Skip to main content

Tagged as

Showing a Separate Tooltip for Each Item in a ListBox - .NET 2.0 (C# Code)

, 2 Jan 2009 CPOL 58.7K 36
Rate this:
Please Sign up or sign in to vote.
This article explains how to show separate tooltips for each item in a Listbox control

Introduction 

ListBoxis a very common control used in Windows Forms. Most often we would need to show some information on each item in the ListBoxwhen the user places the mouse pointer above that item. Sadly there is no straight forward way to do that. The reasons are:

  1. There is no separate mouse hover event for individual items in the list.
  2. As each item in the list is not a control (of course!!), we cannot set a tool tip for items of the list separately. A tooltip can be set only for the ListBoxas such.

But don't give up!! If there is a will, there is a way…

Using the Code

The steps required are explained below:

  1. Drag and drop a ToolTip control into the form having the ListBox. I am calling it ListToolTip.

  2. We have to know the ToolTip text for each item in the ListBox. I have an array of strings as items in the ListBox. So I maintain the ToolTip text for each item in another stringarray. So if I know the index of the Item in the array of items, I can get the corresponding ToolTip text from the ToolTip array using the same index.

  3. Create a handler for the Mouse Move event of the ListBox. Calculate the index of the item over which the mouse pointer is placed now. (This is the tricky part!!)

    1. Calculate the Offset of the item from the top of the list box. This can be obtained by dividing the Y coordinate of the house position by the height of each item. Height of each item is a property of the list box:

      itemIndex = e.Y / objListBox.ItemHeight;
    2. But the topmost item will not always be the first item in the list. If there is a scrollbar and user scrolls down, then the topmost item visible will change. This means that the index obtained above is not the index of the required item in the list of items. Fortunately ListBoxhas another property called TopIndex. If we add the TopIndexwith the index obtained in the above step, then we will get the correct index.

      itemIndex += objListBox.TopIndex;
  4. Now we know the index of the item over which the mouse pointer is placed. Just take the Tooltip of that item from the Tooltip array and associate it with the ToolTip control.

    if ((itemIndex >= 0) && (itemIndex < m_arrItemToolTips.Length))
     {
         ListToolTip.SetToolTip(objListBox, m_arrItemToolTips[itemIndex]);
     }
  5. If the calculated index is out of the range of valid indexes, hide the ToolTip.

      else
    {
    ListToolTip.Hide(objListBox);
    }

The entire code for the mouse move event handler is given below:

private void ItemsListBox_MouseMove(object sender, MouseEventArgs e)
{
     try
     {
         ListBox objListBox = (ListBox)sender;
         int itemIndex = -1;
         if (m_arrItemToolTips != null)
         {
              if (objListBox.ItemHeight != 0)
              {                        
                   itemIndex = e.Y / objListBox.ItemHeight;
                   itemIndex += objListBox.TopIndex;
               }
               if ((itemIndex >= 0) && (itemIndex < m_arrItemToolTips.Length))
               {
                   ListToolTip.SetToolTip(objListBox, m_arrItemToolTips[itemIndex]);
               }
               else
               {
                   ListToolTip.Hide(objListBox);
               }
          }
      }
      catch (Exception ex)
      {                
      }
}

History

  • 2nd January, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Geek13
India India
Eat, Sleep and Drink Code!

You may also be interested in...

Comments and Discussions

 
QuestionShow ToolTip only partially visible item Pin
Member 104551766-Jun-14 5:23
memberMember 104551766-Jun-14 5:23 
AnswerRe: Show ToolTip only partially visible item Pin
hdbbdh26-Apr-15 22:45
memberhdbbdh26-Apr-15 22:45 
SuggestionFlickering - solved Pin
Zephyk9-Jan-12 23:12
memberZephyk9-Jan-12 23:12 
GeneralRe: Flickering - solved Pin
Sean L. Palmer24-Aug-14 17:39
memberSean L. Palmer24-Aug-14 17:39 
QuestionWhere is the download ? Pin
itsho11-Jul-11 10:26
memberitsho11-Jul-11 10:26 
GeneralMy vote of 2 Pin
zwaen8-Apr-10 5:15
memberzwaen8-Apr-10 5:15 
GeneralA possible improvement? [modified] Pin
Sheridan1uk2-Sep-09 11:03
memberSheridan1uk2-Sep-09 11:03 
GeneralRe: A possible improvement? Pin
rhennecke1-Oct-10 1:49
memberrhennecke1-Oct-10 1:49 
GeneralFlicker problem Pin
carlfried2-Jun-09 4:16
membercarlfried2-Jun-09 4:16 
GeneralVB.NET Pin
lanoucm21-May-09 7:02
memberlanoucm21-May-09 7:02 
GeneralWould be better with source Pin
Donsw16-Feb-09 15:46
memberDonsw16-Feb-09 15:46 
GeneralIndex from mouse position Pin
TrendyTim6-Jan-09 16:10
memberTrendyTim6-Jan-09 16:10 
GeneralRe: Index from mouse position Pin
stixoffire10-Jan-09 11:07
memberstixoffire10-Jan-09 11:07 
GeneralRe: Index from mouse position Pin
Sean L. Palmer24-Aug-14 17:45
memberSean L. Palmer24-Aug-14 17:45 
GeneralThanks Pin
tamash_ionut2-Jan-09 9:36
membertamash_ionut2-Jan-09 9:36 
GeneralThanks for the article. Pin
thompsons2-Jan-09 7:58
memberthompsons2-Jan-09 7:58 
GeneralRe: Thanks for the article. Pin
Geek133-Jan-09 0:54
memberGeek133-Jan-09 0:54 

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.150804.4 | Last Updated 2 Jan 2009
Article Copyright 2009 by Geek13
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid