4

I have a listview, and in one of the columns (not the first) I want to display an error code.

What I haven't been able to do is get the ToolTip to display. I have

this.lstList.ShowItemToolTips = true;
...
ListViewItem value = lstList.Items.Add(name, name, 0);
...
if (lstList.Columns.Contains(lstColErrorCode))
{                                
  value.SubItems.Add(new ListViewItem.ListViewSubItem(value, errorCode.ToString()));
  value.ToolTipText = errorCode.ToString("X");
}

I would like to get the hex value of the code to be shown on the tooltip above the decimal value, but it shows above the name.

I haven't been able to get anything I tried to work (like trying to get the coordinates of the subitem). I would appreciate any suggestion.

Dan
  • 9,717
  • 4
  • 47
  • 65
Thalia
  • 13,637
  • 22
  • 96
  • 190

1 Answers1

5

this code works for me

ToolTip toolTip1 = new ToolTip();

void initMethod()
{
    lstList.MouseMove += new MouseEventHandler(lstList_MouseMove);//mousemove handler
    this.lstList.ShowItemToolTips = true;            
    toolTip1.SetToolTip(lstList,"");// init the tooltip
    ...
    ListViewItem value = lstList.Items.Add(name, name, 0);
    ...
    if (lstList.Columns.Contains(lstColErrorCode))
    {                                
        ListViewItem.ListViewSubItem lvs =  value.SubItems.Add(new ListViewItem.ListViewSubItem(value, errorCode.ToString()));
        lvs.Tag = "mydecimal"; // only the decimal subitem will be tooltiped
    }
}

the mousemove event from the listview:

void lstList_MouseMove(object sender, MouseEventArgs e)
{
    ListViewItem item = lstList.GetItemAt(e.X, e.Y);
    ListViewHitTestInfo info = lstList.HitTest(e.X, e.Y);

    if ((item != null) && (info.SubItem != null) && (info.SubItem.Tag!=null) && (info.SubItem.Tag.ToString() == "mydecimal"))
    {
        toolTip1.SetToolTip(lstList,((decimal)info.SubItem.Text).ToString("X"));
    }
    else
    {
        toolTip1.SetToolTip(lstList, "");
    }
}
shytikov
  • 9,155
  • 8
  • 56
  • 103
S3ddi9
  • 2,121
  • 2
  • 20
  • 34
  • 1
    Unfortunately I was not able to get this to work - not only the tooltip will not show as I want (even a simple toolTip1.SetToolTip(lstList, "hello"); will not work), but I get tooltips on all items except for the name. Yet the MouseMove works - I had to add an additional check for null for info.SubItem.Tag for the name object, because it was giving an exception when touched by the mouse... Is there something special about listview, based on the VS version perhaps ? – Thalia Aug 07 '12 at 07:44
  • no exception either, are you sure that you added all my code lines, try to create a new project then try it may be you can figure out what's wrong – S3ddi9 Aug 07 '12 at 08:02
  • I have checked, every single item. I will make a small project - tomorrow. Your solution is very logical, it will have o work, I will play with it and find my bug. Thank you. – Thalia Aug 07 '12 at 08:04
  • Ok ah i've missed a cast (I updated my answer), hope this will work – S3ddi9 Aug 07 '12 at 16:47
  • I removed the "item" - it was always null. Thanks a lot for your help. – Thalia Aug 08 '12 at 19:04
  • Using this code, my tooltips are flickering. Even though I stop cursor movement. any ideas ? – Ahmed Jan 08 '13 at 23:35