none
Cannot get the tooltip balloon to show on a listviewitem

    Question

  • I have multiple controls on a windows Form and I get the balloon for all my labels but I always get the standard rectangular box for listviewitems. Is there any way to get around this?

    the listview view mode is set at details if that helps

    thanks in advance

     

    Friday, September 08, 2006 7:40 PM

Answers

  • Actually what happens is this: The tooltip works on controls on the form using the mouse move, however listview controls' MouseMove is captured by the listview's items, and these do not use the tooltip control you added. The code will not show any tooltip if you mouse-over an empty area of the listview or if the listview is empty! Only when the mouse is over an item will a tooltip show! However, if you want it to show a tooltip when the mouse is over an empty area of the listview, make the following modification:

    if (loc != e.Location) {

    loc = e.Location;
    ListViewItem it = listView1.GetItemAt(loc.X, loc.Y);
    string toolTipString = it.ToolTipText;
    if (it != null) { toolTip1.SetToolTip(listView1, toolTipString); }
    else{toolTip1.SetToolTip(listView1, "Some default tooltip"); }

    }

    If you want the list view to always show the same tooltip, I think this will work:

    if (loc != e.Location) {

    loc = e.Location;
    toolTip1.SetToolTip(listView1, "Some default tooltip");

    }

    I tried the first two on a form with a list view and it worked just fine. Hope this helps!

    Monday, September 11, 2006 10:09 PM

All replies

  • Try the following:
    1. Add a Point field to your form:
    private Point loc;
    2. Add an event handler to the list view control's MouseMove event:
    private void listView1_MouseMove(object sender, MouseEventArgs e) {

    if (loc != e.Location) {

    loc = e.Location;
    ListViewItem it = listView1.GetItemAt(loc.X, loc.Y);
    string toolTipString = it.ToolTipText;
    if (it != null) { toolTip1.SetToolTip(listView1, toolTipString); }

    }

    }

    Of course you'll have to define the tooltiptext property for all your listview items. You can also replace the toolTipString assignment line to create the tooltip text dynamically (maybe from an array and maybe from an XML file! whatever you want, you now have a reference to the hovered listview item and you can do whatever process to define the tool tip string for that specific item). You should also have a tool tip component in your form with the IsBallon property set to true.

    The UI is not the best, it might give you a little trouble, I'm working on a fine tuning to it!

    Hope this helped!

    Saturday, September 09, 2006 7:40 AM
  • Well actually, using your code does not make any tool tip appear anymore for any listviewitem.

    Like I mentionned, my problem was not getting the tooltip info to show but getting the balloon to show. I keep get the rectangle even though the IsBalloon property is set at True for the listviewitem. All the other controls on the Form show the balloon.

    Thanks for the try.

     

    Monday, September 11, 2006 5:03 PM
  • Here is a solution that may work...
     
    You can just have a generic toolTip object (just drag it into your form from the toolbox), set the toolTip object on the listview, and depending on which item it is over on the listview (use mouseposition to check), change the toolTip text to what you need.
     
    In order to do this, you would have to detect mouse movement on the listview object. To do so, add this line to your Form.designer.cs
     
    ----
    this.listView1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseMove);
    ----

    Then you can add the event handler for the mouse move. To check which item in the listview the mouse is currently over, you can just run a for loop through each item, and check that both the x and y coordinates of the mouse are within the item's bounds. Here is what I wrote up for you ...

    ----
    int prevX = 0;
    int prevY = 0;
    private void listView1_MouseMove(object sender, MouseEventArgs e)
    {
      bool toolTipSet = false;
      if (prevX == e.X && prevY == e.Y)
        return;
      for (int i = 0; i < listView1.Items.Count; i++)
      {
        if (e.X > listView1.Items[ i ].Position.X && e.X < listView1.Items[ i ].Bounds.Right &&
          e.Y > listView1.Items[ i ].Position.Y && e.Y < listView1.Items[ i ].Bounds.Bottom)
        {
          toolTip1.SetToolTip(listView1, "Item" + (i + 1));
          toolTipSet = true;
          break;
        }
      }
      if (toolTipSet == false)
        toolTip1.SetToolTip(listView1, null); 
      prevX = e.X;
      prevY = e.Y;
    }
    ----
     
    Also, in the constructor of the Form, you should add these lines to set up the tooltip ...
     
    ----
    toolTip1.IsBalloon = true;
    toolTip1.InitialDelay = 2000;
    toolTip1.AutoPopDelay = 3000;
    ----
     
    Just to explain some of the code...
     
     - For the boolean value "toolTipSet", I use it to check whether any of the items in the toolTip actually had the mouse over it. If not, then I just set it so it will show no tooltip by passing in a null string. (for when the mouse is in the listview but not over any of the items - we don't want to show any tooltip)
     - You should replace the line where I wrote "toolTip1.SetToolTip(listView1, "Item" + (i + 1));" in order to have it actually display the toolTip you want for each item (maybe use a switch/case block or something - if the tooltip is dynamic and will be generated on the fly, just call a function here to get the string you would use for that tooltip).
     - The code where I check the PrevX and PrevY values ... I just added that because it seemed that whenever the popup appeared, it would set off the mouse move event and that would reset the tooltip again, which in turn would fire the event again and etc. This was causing a flicker on the tooltip and I just thought I'd check to see if the mouse position was still in the same spot and if so, I would just abort checking and updating the tooltip.
     
    As a final note...
     
    I didn't spend too much time writing up the code ... so at the moment, when you try to run the code, it doesn't look quite right and it's kinda buggy. The tooltip kind of flickers when you mouseover it and it seems like it kind of shows and hides itself kind of randomly. You still need to fix the code to get it looking right. I just wrote the code to help you get an idea on how to get started, and as I have other stuff to work on, I'll leave it to you mess around with it and see if you can get what you need. I hope this at least helps you get on the right track. Good luck. 
     
     
    Monday, September 11, 2006 8:04 PM
  • Actually what happens is this: The tooltip works on controls on the form using the mouse move, however listview controls' MouseMove is captured by the listview's items, and these do not use the tooltip control you added. The code will not show any tooltip if you mouse-over an empty area of the listview or if the listview is empty! Only when the mouse is over an item will a tooltip show! However, if you want it to show a tooltip when the mouse is over an empty area of the listview, make the following modification:

    if (loc != e.Location) {

    loc = e.Location;
    ListViewItem it = listView1.GetItemAt(loc.X, loc.Y);
    string toolTipString = it.ToolTipText;
    if (it != null) { toolTip1.SetToolTip(listView1, toolTipString); }
    else{toolTip1.SetToolTip(listView1, "Some default tooltip"); }

    }

    If you want the list view to always show the same tooltip, I think this will work:

    if (loc != e.Location) {

    loc = e.Location;
    toolTip1.SetToolTip(listView1, "Some default tooltip");

    }

    I tried the first two on a form with a list view and it worked just fine. Hope this helps!

    Monday, September 11, 2006 10:09 PM
  • I forgot to reset the showtooltipitem at False and it now works. But still some flickering like you said.

    I also put the "string toolTipString = it.ToolTipText;" inside the if (it!=null) because it gave me an exception.

    Besides the flickering, it works great. A lot of work for nothing though. If only it was standard.

    Thanks

     

     

    Tuesday, September 12, 2006 7:12 PM
  • Anyone know how to stop the 'flickering' with the Balloon Icon?
    Wednesday, November 01, 2006 7:55 PM
  • The solution is fairly simple in VB.net

     

    Add a Tooltip Control to a form called ToolTip1 and a ListView called MyListView then put this one-line in the ItemMouseHover event. Make the tooltip control a balloon and set the icon.

     

    Make sure you put a ToolTopText value into every list item or add an IF statement to the event. Any time the mouse pauses over a listview item, the tooltip balloon is activated.

     

    Private Sub MyListView_ItemMouseHover(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemMouseHoverEventArgs) Handles RoutesListView.ItemMouseHover

    Me.ToolTip1.SetToolTip(Me.MyListView, e.Item.ToolTipText)

    End Sub

     

     

    Wednesday, April 09, 2008 10:48 AM