简体   繁体   中英

How to create a clickable links in ListView?

I am trying to make URL clickable inside of ListView item.

How do I do this?

The way I want it to work is to user store link in plain text, and then when I am retrieving the links I want to make them clickable in ListView.

This is how I retrieve entries from my database while read.GetString(2) pulls the URL value:

if (security.DecryptAES
    (read.GetString(1), storedAuth.Password, 
    storedAuth.UserName) == "Web Site Password")
{
    // Count Web passwords.
    countWeb++;
    Web = new ListViewItem("");
    Web.SubItems.Add(security.DecryptAES
        (read.GetString(2), storedAuth.Password, storedAuth.UserName));
    Web.SubItems.Add(security.DecryptAES
        (read.GetString(5), storedAuth.Password, storedAuth.UserName));
    Web.SubItems.Add(security.DecryptAES
        (read.GetString(6), storedAuth.Password, storedAuth.UserName));
    Web.Tag = read.GetInt32(0);
    lvWeb.Items.Add(Web);
}

The first thing you want to do is give visual feedback to let the user know that the item is clickable. I'll just arbitrarily assume the url is in the 2nd column. Add the MouseMove event for the ListView:

    private void listView1_MouseMove(object sender, MouseEventArgs e) {
        var hit = listView1.HitTest(e.Location);
        if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1]) listView1.Cursor = Cursors.Hand;
        else listView1.Cursor = Cursors.Default;
    }

The next step is very similar, implement the MouseUp event to detect a click on the sub-item:

    private void listView1_MouseUp(object sender, MouseEventArgs e) {
        var hit = listView1.HitTest(e.Location);
        if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1]) {
            var url = new Uri(hit.SubItem.Text);
            // etc..
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM