将listview结果转换为超链接

时间:2015-10-20 12:34:18

标签: c# listview windows-forms-designer

我想将Arr [2]中返回的每个结果转换为超链接。我不知道从哪里开始。

    public void TransactionLogSell()
    {

        listView1.View = View.Details;
        listView1.GridLines = true;
        listView1.FullRowSelect = true;

        listView1.Columns.Add("Buy Order Only", 97);
        listView1.Columns.Add("Amount", 95);
        listView1.Columns.Add("Transaction ID", 100);



        string[] arr = new string[3];
        ListViewItem item;

        string URT = "https://api.eveonline.com/char/WalletTransactions.xml.aspx?keyID=4602486&&vCODE=BHGVeXQkRLKLkIkZQHdeyUxmUz9EfUwbvGzoc2eO4ZR8kRMYxk8PbD4LMwLF7BvH";
        XmlDocument XMLtrans = new XmlDocument();
        XMLtrans.Load(URT);
        XmlNodeList TRnodelist = XMLtrans.SelectNodes("/eveapi/result/rowset/row[@transactionType='sell']");
        foreach (XmlNode xmlnode in TRnodelist)
        {
            if (xmlnode.Attributes["transactionType"] != null)
                arr[0] = xmlnode.Attributes["transactionType"].InnerText;
            if (xmlnode.Attributes["price"] != null)
                arr[1] = xmlnode.Attributes["price"].InnerText;
            if (xmlnode.Attributes["transactionID"] != null)
                arr[2] = xmlnode.Attributes["transactionID"].InnerText;
            double amount = 0.0d;
            if (Double.TryParse(arr[1], NumberStyles.Currency, null, out amount))
            {
                arr[1] = amount.ToString("C3", CultureInfo.CreateSpecificCulture("ja-JP"));
            }
            item = new ListViewItem(arr);
            listView1.Items.Add(item);
        }
    }

我已经尝试了以下内容;

    listView1.HotTracking = true;

这使我的所有列成为超链接。

这是一张照片;

enter image description here

注意:复制事件代码时,请不要忘记hook up事件!

1 个答案:

答案 0 :(得分:0)

这是一个例子。它通过改变鼠标光标给用户反馈;比绘画项目简单得多。

private void listView1_MouseMove(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo HI = listView1.HitTest(e.Location);
    if (HI.SubItem != null && HI.SubItem.Name == "Link")
         Cursor = Cursors.Hand;
    else Cursor = Cursors.Default;
}

private void listView1_MouseUp(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo HI = listView1.HitTest(e.Location);
    if (HI.SubItem != null && HI.SubItem.Name == "Link")
        MessageBox.Show("You have clicked on " + HI.SubItem.Tag.ToString());
    // do your stuff instead!
}

对于我的测试,我准备了一个3列ListView,如下所示:

for (int i = 0; i < 5; i++)
{
    ListViewItem lvi = new ListViewItem("I" + i);
    ListViewItem.ListViewSubItem lvsi = lvi.SubItems.Add("Link " + i);
    lvi.SubItems.Add("S" + i);
    lvsi.Name = "Link";
    lvsi.Tag  = "Test # " + i;
    listView1.Items.Add(lvi);
}

请注意,我已将SubItemName中的每个Tag分配给MouseClickListView,以识别它并保存您可以使用的数据。

另请注意,MouseDown仅在点击MouseUp中的第一个列时有效; DrawXXXe.DrawDefault = true;始终有效..

要显示带下划线和蓝色的链接项(我们在此处),您需要拥有它们。为了让标记再次消失,你需要跟踪。您必须对所有三个DrawSubItem事件进行编码,主要是设置e.ColumnIndex仅在index 1中,您需要检查int CurrHitRow = -1; int LastHitRow = -1; Point curMouse = Point.Empty; 是否为您的链接列;我使用第二列,即MouseMove ..

一个简单的例子:

enter image description here

显示蓝色链接是因为鼠标在它上面;鼠标只是没有在截图中捕获..

我们声明了一些辅助变量:

private void listView1_MouseMove(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo HI = listView1.HitTest(e.Location);
    if (HI.SubItem != null && HI.SubItem.Name == "Link")
    {
        if (CurrHitRow != LastHitRow) LastHitRow = CurrHitRow;
        CurrHitRow = HI.Item.Index;
        if (LastHitRow >= 0 && CurrHitRow != LastHitRow) 
            listView1.Invalidate();
        Cursor = Cursors.Hand; 
    }
    else Cursor = Cursors.Default;
    curMouse = e.Location;
}

我们扩展private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { e.DrawDefault = true; } private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { e.DrawDefault = true; } 以跟踪光标下的项目:

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    if (e.ColumnIndex != 1) e.DrawDefault = true;
    else 
    {
        ListViewHitTestInfo HI = listView1.HitTest(curMouse);
        if (HI.Item == null) { e.DrawDefault = true; return; }
        bool showLink = e.SubItem.Text.Substring(0,4) == "Link" 
                     && e.Item.Index == HI.Item.Index;

        e.DrawBackground();
        using (Font font = new Font(listView1.Font, showLink ?
                                    FontStyle.Underline : FontStyle.Regular))
        {
            e.Graphics.DrawString(e.SubItem.Text, font, showLink ?
              SystemBrushes.HotTrack : SystemBrushes.ControlText, e.Bounds);
        }
    }
}

我们添加了无聊的事件:

lvi.UseItemStyleForSubItems = false;

并不那么无聊:

DrawSubItem

最后我们需要将其添加到设置中:

DrawItem

或者单独的TextRenderer.DrawText不会被调用。相反,您可以将其与Graphics.DrawString事件合并..

请注意,有些人更喜欢$(window).resize(function() { if ($(window).height() < 500) { $('.element').addClass('className'); } } 而不是use,有关示例,请参阅here!但我从未发现其中的差别令人信服......