Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I display links in a ListView's detail mode?

I'm displaying a set of search results in a ListView. The first column holds the search term, and the second shows the number of matches.

There are tens of thousands of rows, so the ListView is in virtual mode.

I'd like to change this so that the second column shows the matches as hyperlinks, in the same way as a LinkLabel shows links; when the user clicks on the link, I'd like to receive an event that will let me open up the match elsewhere in our application.

Is this possible, and if so, how?

EDIT: I don't think I've been sufficiently clear - I want multiple hyperlinks in a single column, just as it is possible to have multiple hyperlinks in a single LinkLabel.

like image 665
Simon Avatar asked Dec 13 '22 23:12

Simon


2 Answers

You can easily fake it. Ensure that the list view items you add have UseItemStyleForSubItems = false so that you can set the sub-item's ForeColor to blue. Implement the MouseMove event so you can underline the "link" and change the cursor. For example:

ListViewItem.ListViewSubItem mSelected;

private void listView1_MouseMove(object sender, MouseEventArgs e) {
  var info = listView1.HitTest(e.Location);
  if (info.SubItem == mSelected) return;
  if (mSelected != null) mSelected.Font = listView1.Font;
  mSelected = null;
  listView1.Cursor = Cursors.Default;
  if (info.SubItem != null && info.Item.SubItems[1] == info.SubItem) {
    info.SubItem.Font = new Font(info.SubItem.Font, FontStyle.Underline);
    listView1.Cursor = Cursors.Hand;
    mSelected = info.SubItem;
  }
}

Note that this snippet checks if the 2nd column is hovered, tweak as needed.

like image 117
Hans Passant Avatar answered Dec 24 '22 12:12

Hans Passant


Use ObjectListView -- an open source wrapper around a standard ListView. It supports links directly:

alt text

This recipe documents the (very simple) process and how you can customise it.

like image 42
Grammarian Avatar answered Dec 24 '22 12:12

Grammarian