0

I want to set the tooltip for items present in the List Box when they are hovered over. I am using the following code from this question : How can I set different Tooltip text for each item in a listbox?

    private ITypeOfObjectsBoundToListBox DetermineHoveredItem()
{
    Point screenPosition = ListBox.MousePosition;
    Point listBoxClientAreaPosition = listBox.PointToClient(screenPosition);

    int hoveredIndex = listBox.IndexFromPoint(listBoxClientAreaPosition);
    if (hoveredIndex != -1)
        return listBox.Items[hoveredIndex] as ITypeOfObjectsBoundToListBox;
    else
        return null;        
}

The hovered index is always -1 and as a result I am getting null. Any suggestions..

Community
  • 1
  • 1
  • Have you tried using a `ListView` or even a `DataGridView` Instead of `ListBox`. Then you can simply assign different tooltip for each item. – Reza Aghaei Oct 10 '16 at 16:58

1 Answers1

0

For example you can use a listview:

  • Set the ListView's ShowItemToolTips property to true.

example of code for creating new items with tooltip:

    public Form1()
    {
        InitializeComponent();

        ListViewItem item1WithToolTip = new ListViewItem("Item with a tooltip"); // new item for listview1
        item1WithToolTip.ToolTipText = "This is the item tooltip."; // set tooltip text
        item1WithToolTip.SubItems.Add("1"); // add item
        item1WithToolTip.SubItems.Add("3");

        ListViewItem item2WithToolTip = new ListViewItem("Second item with a tooltip"); // new item for listview1
        item2WithToolTip.ToolTipText = "A different tooltip for this item.";
        item2WithToolTip.SubItems.Add("1");
        item2WithToolTip.SubItems.Add("2");

        listView1.Items.Add(item1WithToolTip);
        listView1.Items.Add(item2WithToolTip);
     }

Now add a listview to your form and see the result of this code:

enter image description here

Timon Post
  • 2,779
  • 1
  • 17
  • 32