Can I display links in ListView details mode?

I am showing a set of search results in a ListView . The first column contains the search query, and the second shows the number of matches.

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

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

Is this possible, and if so, how?

EDIT: I don't think I was clear enough - I need several hyperlinks in one column, just as you can have multiple hyperlinks in one LinkLabel .

+4
source share
5 answers

You can easily fake it. Make sure the list items you add have UseItemStyleForSubItems = false so that you can set the ForeColor subitem to blue. Add a MouseMove event to underline the link and change the cursor. For instance:

 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 freezes, adjust it as needed.

+8
source

Use ObjectListView , an open source wrapper around the standard ListView. It directly supports links:

alt text

This recipe documents the (very simple) process and how it can be customized.

+3
source

The other answers here are great, but if you don't want to need to crack the code together, check out DataGridView , which supports LinkLabel equivalent columns.

Using this control, you get all the functionality for presenting parts in a ListView , but with more customization per line.

+1
source

You can, by inheriting from ListView , override the OnDrawSubItem method.
Here is a VERY simple example of how you can do this:

 public class MyListView : ListView { private Brush m_brush; private Pen m_pen; public MyListView() { this.OwnerDraw = true; m_brush = new SolidBrush(Color.Blue); m_pen = new Pen(m_brush) } protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e) { e.DrawDefault = true; } protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e) { if (e.ColumnIndex != 1) { e.DrawDefault = true; return; } // Draw the item background. e.DrawBackground(); var textSize = e.Graphics.MeasureString(e.SubItem.Text, e.SubItem.Font); var textY = e.Bounds.Y + ((e.Bounds.Height - textSize.Height) / 2); int textX = e.SubItem.Bounds.Location.X; var lineY = textY + textSize.Height; // Do the drawing of the underlined text. e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, m_brush, textX, textY); e.Graphics.DrawLine(m_pen, textX, lineY, textX + textSize.Width, lineY); } } 
+1
source

You can set HotTracking to true so that when the user hovers over an item, it is displayed as a link.

0
source

Source: https://habr.com/ru/post/1301691/


All Articles