views:

194

answers:

5

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.

A: 

You can set HotTracking to true so that when the user hovers mouse over the item it appears as link.

Giorgi
Doesn't that just do it for each row? I want to do it for individual text ranges in a subitem.
Simon
+1  A: 

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.

Hans Passant
+1  A: 

The other answers here are great, but if you don't want to have to hack some code together, look at the DataGridView control which has support for LinkLabel equivalent columns.

Using this control, you get all the functionality of the details view in a ListView, but with more customisation per row.

Codesleuth
The DataGridViewLinkCell only shows a single link - it won't let you do multiple links like a LinkLabel will.
Simon
+1  A: 

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

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's 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);
    }
}
Patrik
+1  A: 

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.

Grammarian