none
list box tooltip? RRS feed

  • Question

  • Hi.

    Is it possible on a WinForm C# application to have a tooltip appear under the mouse cursor when you hover over an item in a listbox?

    I am using .NET 2.0

    Saturday, November 26, 2005 7:11 PM
    Moderator

Answers

  • Yes you can but it requires a little bit more work than using something like a ListView.  You'll first need to drop a ToolTip component onto the parent form.  This gives your ListBox tool tip capability.  Then you'll need to trap the MouseMove event on your list box.  Whenever this event occurs you should get the index of the item at the current position of the mouse.  If you get an item then you can use whatever logic is appropriate to determine the tool tip to display.  Then simply set the tooltip of the list view to that text like so:

    private void OnListBoxMouseMove ( object sender, MouseEventArgs e )
            {
                string strTip = "";

                //Get the item
                int nIdx = listBox1.IndexFromPoint(e.Location);
                if ((nIdx >= 0) && (nIdx < listBox1.Items.Count))
                    strTip = listBox1.Items[nIdx].ToString();

                toolTip1.SetToolTip(listBox1, strTip);
            }

    This code sets the tooltip to the text of the item under the cursor.  In this case the code is sitting on the form so I had access to the tool tip directly.  Alternatively you could create a class deriving from ListBox that embeds the tool tip in it.

    BTW, WinForm related questions should be posted to the Windows Forms forum.  The people there are better suited to answering questions like these so you'll stand a better chance of getting a good response.  This forum is more for C#-specific questions. 

    Good luck,
    Michael Taylor - 11/26/05
    Saturday, November 26, 2005 8:39 PM

All replies

  • Yes you can but it requires a little bit more work than using something like a ListView.  You'll first need to drop a ToolTip component onto the parent form.  This gives your ListBox tool tip capability.  Then you'll need to trap the MouseMove event on your list box.  Whenever this event occurs you should get the index of the item at the current position of the mouse.  If you get an item then you can use whatever logic is appropriate to determine the tool tip to display.  Then simply set the tooltip of the list view to that text like so:

    private void OnListBoxMouseMove ( object sender, MouseEventArgs e )
            {
                string strTip = "";

                //Get the item
                int nIdx = listBox1.IndexFromPoint(e.Location);
                if ((nIdx >= 0) && (nIdx < listBox1.Items.Count))
                    strTip = listBox1.Items[nIdx].ToString();

                toolTip1.SetToolTip(listBox1, strTip);
            }

    This code sets the tooltip to the text of the item under the cursor.  In this case the code is sitting on the form so I had access to the tool tip directly.  Alternatively you could create a class deriving from ListBox that embeds the tool tip in it.

    BTW, WinForm related questions should be posted to the Windows Forms forum.  The people there are better suited to answering questions like these so you'll stand a better chance of getting a good response.  This forum is more for C#-specific questions. 

    Good luck,
    Michael Taylor - 11/26/05
    Saturday, November 26, 2005 8:39 PM
  • Thank-you :-)
    Saturday, November 26, 2005 10:05 PM
    Moderator
  • The answer from CoolDadTx works, but the tooltip will flicker.

     

    Declare a new ToolTip and initialize it.

    ToolTip toolTip = new ToolTip();
    

    Next add in a ListBox in the designer or the same way you did with ToolTip.

    Now just set the listboxe's MouseMove event to this function (listBox is our ListBox's name):

    private void listBox_OnMouseMove(MouseEventArgs e)
    {
         // Get the index for the item being hovered.
         int index = listBox.IndexFromPoint(e.Location);
    
         // Check if the index is valid.
         if (index != -1 && index < listBox.Items.Count)
         {
              // Check if the ToolTip's text isn't already the one
              // we are now processing.
              if (toolTip.GetToolTip(listBox) != listBox[index].ToString())
              {
                   // If it isn't, then a new item needs to be
                   // displayed on the toolTip. Update it.
                   toolTip.SetToolTip(listBox, listBox.Items[index].ToString();
              }
         }
    }
    

    The ToolTip will update when the mouse hovers a new item.

    Monday, January 9, 2012 9:22 PM
  • Good. Its working fine. 

    One more suggestion...

    Now it is showing ToolTip even in ListBox empty areas (free space under last list box item). We can avoid this by a simple check.

    private void listBoxMouseMoved(object sender, MouseEventArgs e)
             {
                 int index = listBox.IndexFromPoint(e.Location);
    
                 if (index != -1 && index < listBox.Items.Count)
                 {
                     if (toolTip.GetToolTip(listBox) != listBox.Items[index].ToString())
                     {
                         toolTip.SetToolTip(listBox, listBox.Items[index].ToString());
                     }
                 }
                 else
                 {
                     toolTip.SetToolTip(this.listBox, string.Empty);
                 }
             }

    • Proposed as answer by Vysakh Lenin Tuesday, April 16, 2013 11:19 AM
    Thursday, April 4, 2013 4:40 AM
  • it is continuously blinking in my code is is often happance or may be beacause my listbox and tool tip are defined in c#

    Friday, March 27, 2015 6:50 AM