【问题标题】:How to create a clickable links in ListView?如何在 ListView 中创建可点击的链接?
【发布时间】:2012-01-24 01:01:39
【问题描述】:

我正在尝试使 URL 在 ListView 项内可点击。

我该怎么做?

我希望它工作的方式是用户以纯文本形式存储链接,然后当我检索链接时,我想让它们在 ListView 中可点击。

这就是我在 read.GetString(2) 提取 URL 值时从数据库中检索条目的方式:

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);
}

【问题讨论】:

    标签: c# winforms url listview


    【解决方案1】:

    您要做的第一件事是提供视觉反馈,让用户知道该项目是可点击的。我只是随意假设网址在第二列。为 ListView 添加 MouseMove 事件:

        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;
        }
    

    下一步很相似,实现MouseUp事件检测子项的点击:

        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..
            }
        }
    

    【讨论】:

    • 这看起来很不错。那么 Uri 是创建 URL 的对象吗?
    • 好的,我已经使用您的代码将手悬停在我想要的字段上。但是当我点击什么都没有发生。那应该是鼠标点击吗?
    • 使用调试器。您应该在代码中填写 // etc..,以防万一。
    • 啊!知道了,在这种情况下,我可以添加行:System.Diagnostics.Process.Start(url.ToString()); 并且它可以工作。我需要做的只是警告先打开一个站点。谢谢!
    猜你喜欢
    • 2014-03-04
    • 2020-06-28
    • 2016-07-31
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多