简体   繁体   中英

Strange behavior of a Tooltip on a Listbox

I'm brand new to C# and I wanted to enabled tooltips on a listbox. My wish was to be able to display a different tooltip depending on the hovered item in the listbox (the standard listbox's behavior is to display one single tooltip for itself no matter what item is hovered)...

I managed to write the following code :

private void MyInheritedListBox_MouseMove(object _sender, MouseEventArgs _event)
{
  int itemIndex = -1;
  itemIndex = this.IndexFromPoint(new Point(_event.X, _event.Y));

  Console.WriteLine(_event.X.ToString() + " - " + _event.Y.ToString());

  if (itemIndex >= 0 && itemIndex != currentHoveredIndex)
  {
    string l_sMyItemString = (string)this.Items[itemIndex];
    MyToolTip.Show(l_sMyItemString, this);

    currentHoveredIndex = itemIndex;
  }

  if (currentHoveredIndex == -1)
  {
    MyToolTip.Hide(this);
  }
}

private void MyInheritedListBox_MouseLeave(object sender, EventArgs e)
{
  currentHoveredIndex = -1;
}

My problem is, at when the application first starts, it's working like a charm... When moving my mouse, the tooltip follows it and adapts itself to the hovered item. Fine.

But after playing a bit with it (few seconds), or leaving the listbox with the mouse cursor, and when entering again the listbox, I ain't got any tooltips anymore... I play for a few seconds and then I get one again but the tooltip is not following the cursor anymore... My first guess would be that it's a "display" behavior but I can't manage to find how to correct it...

Do you guys have any ideas ?

Thanks in advance !

Instead of

MyToolTip.Show(l_sMyItemString, this); 

Try

MyToolTip.SetToolTip(this, l_sMyItemString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM