Controls - Do you use a ToolTip to show the full text of hidden ListView data?

Last updated by Thomas Iwainski [SSW] 10 months ago.See history

When you can't see all the text for an item in a ListView you need to expose the full text via a ToolTip.

Figure: Bad example - Users can't see all the text and the ListView doesn't use a Tooltip

Figure: Good example - Users can't see all the text, but the ListView shows all the text via a Tooltip

The code to do this is:

private ListViewItem hoveredItem;

private void listView1_MouseMove(object sender, MouseEventArgs e) {
  ListView lv = (ListView) sender;
  ListViewItem item = lv.GetItemAt(e.X, e.Y);
  int columnIndex = 1;
  if (item != hoveredItem) {
    hoveredItem = item;
    if (item == null) {
      toolTip1.SetToolTip(lv, "");
    } else {
      // Make sure the mouse hovered row has the subitem 
      if (item.SubItems.Count > columnIndex) {
        toolTip1.SetToolTip(lv, item.SubItems[columnIndex].Text);
      } else {
        toolTip1.SetToolTip(lv, "");
      }
    }
  }
}
We open source. Powered by GitHub