0

i want to turn every result returned in Arr[2] into a hyperlink. I got no real idea where to start.

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

i've tried the following;

    listView1.HotTracking = true;

This makes all my columns into hyperlinks.

Here is a picture also;

enter image description here

Note: When copying event code do not forget to hook up the events!

Community
  • 1
  • 1
Losec
  • 105
  • 11
  • You need to decide what things you want to achieve: Clicking and then what? Opening a browser? Also do you need visual feedback? This would involve ownerdrawing the LV, or at least the current (use hittest for this) and the last item; to call a browser you can store the full url in the item/subitems' tags and use startprocess or load a webbrowser control. - LVItems/LVSubItems are derived directly from object and have no event model.. – TaW Oct 20 '15 at 14:18
  • well my plan was make transaction ID a link (in some way) so when click it will open a more detailed view of that transaction (a new LV) – Losec Oct 20 '15 at 14:26
  • Ok, so basically the question is: How to detect the click on a given item/column/row? See my answer for this.. – TaW Oct 20 '15 at 14:41
  • not really, i want to make the Transaction ID into basically a linklabel or something along them lines so when you hover over it appears as underlined. but i shall wait to see your answer to see if that works for me :) thanks bud. – Losec Oct 20 '15 at 14:42
  • I have updated to include blue underlined display of the item under the cursor – TaW Oct 20 '15 at 18:04
  • Have you resolved your problems? – TaW Oct 25 '15 at 20:31
  • no i havn't ! driving me nutes! – Losec Oct 26 '15 at 12:14
  • So what is missing? Did you try my solution(s)? – TaW Oct 26 '15 at 12:59
  • currently in the progress of testing your solution now :) – Losec Oct 28 '15 at 10:59
  • although im struggeling to intergrate it in to my code atm. – Losec Oct 28 '15 at 11:26
  • If you have specific questions, just ask! – TaW Oct 28 '15 at 11:27
  • OKIE DOKIE! i've tried getting this to work with my code but even running it solo in a new form isnt working for me. How does your full form look like when you got this working? – Losec Oct 28 '15 at 11:49
  • It is burried in a huge testform. Let's do a few checks, ok? Did you set the ListView to `OwnerDraw=true ?`Are all events __hooked up__?? Did you make the three variables __class level__ variables??? – TaW Oct 28 '15 at 11:59
  • when i set listview1.ownerdraw = true; it just blanks my vist view – Losec Oct 28 '15 at 12:09
  • Well, that's a start :-) Next you need to [hook up](http://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161#33276161) the three drawXXX events! – TaW Oct 28 '15 at 12:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93588/discussion-between-taw-and-losec). – TaW Oct 28 '15 at 12:28

1 Answers1

0

Here is an example. It gives user feedback by changing the mouse cursor; much simpler than painting the items.

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

For my test I have prepared a 3 column ListView like this:

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

Note that I have given each SubItem in one Column both a Name and a Tag, to identify it and for holding data you can use.

Also note that MouseClick only works when clicking the 1st Column in a ListView; MouseDown and MouseUp always work..

To show the link items underlined and blue (while we're at it) you need to ownerdraw them. And to make the markup go away again you need to keep track. You would have to code all three DrawXXX events, mostly setting e.DrawDefault = true; Only in DrawSubItem you would do a check on the e.ColumnIndex to be your link column; I use the second column, i.e. index 1..

A quick example:

enter image description here

The blue link is shown because the mouse is over it; the mouse just isn't captured in the screenshot..

We declare a few helper variables:

int CurrHitRow = -1;
int LastHitRow = -1;
Point curMouse = Point.Empty;

We expand the MouseMove to keep track of the item under the cursor:

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

We add the boring events:

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
    e.DrawDefault = true;
}

And the not so boring one:

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

Finally we need to add this to the setup:

lvi.UseItemStyleForSubItems = false;

or else the separate DrawSubItem would not get called. Instead you could merge it with the DrawItem event..

Note that some folks greatly prefer TextRenderer.DrawText over Graphics.DrawString, see here for an example! But I never found the difference to be compelling..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111