Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...