Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
- I have a bunch of links in a file that i read and render in my visual list.
I created the LinkLabel object. I created a file with true and false for each link from my link file too. So its a parallel file to the link one. So far i can read from my "visited.txt" file and update my visual list fine. I am updating manually the list. But i want to make it automatic, when i press a link in my visual list, to re-write a List<> and from that my "visited.txt" file.
- I get this problem for 3 days now. Well, i had to resolve other inconveniences too, but it starts to linger and i want it resolved. I really have no (simple) idea how to proceed, since all that i could think of is very complicated. I will do it in the complicated way in the end, but im thinking maybe is a little trick that i miss and you know it (i bet you do) >:)

What I have tried:

C#
        private void buttonImport_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < link.Length; i++)
            {
                LinkLabel linklabel = new LinkLabel();
                linklabel.Location = new Point(0, 22 * (i + 1));
                panel1.Controls.Add(linklabel);

                linklabel.LinkClicked += new LinkLabelLinkClickedEventHandler(linklabel_LinkClicked);

/* This line, it means that it will read the "visited.txt" file and update in my list whatever is saved in the file. And if i modify the content from file manually, is working fine in code and in my visual list. */
                linklabel.LinkVisited = bool.Parse(visited[i]);
            }
        }


        List<string> ListLinksVisited = new List<string>();
        void linklabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (e.Link.LinkData.ToString() != "")
            {
                Process.Start(e.Link.LinkData.ToString());
                e.Link.Visited = true; //show in list that link is clicked

/* These links are dynamically created!  (as you can see from the code provided)
 I want to get the number of the link that is pressed.
 - How to do it? 
 Thank you!*/

            }
        }
Posted
Updated 17-Aug-19 3:50am
v5
Comments
OriginalGriff 17-Aug-19 5:35am    
Code formatting is fully coloured: but you may need to select the source code type in order to enable it for your code.
I've edited your question to add "lang=cs" to your <pre> tags - but you can get the same by using the "code" drop down and selecting "C#".
_Q12_ 17-Aug-19 5:39am    
thank you !
_Q12_ 17-Aug-19 5:48am    
i see my mistake now. I was relying on AUTO formatting where it inserts only \<\pre\> without mentioning about cs. But while this is a c# thread, it should be selected by default. But i will remember from now on what is the correct way. Again, thank you.
BillWoodruff 17-Aug-19 6:09am    
Are you using the capacity of a LinkLabel Control to contain multiple links ?

Is this Windows Forms ?
_Q12_ 17-Aug-19 6:23am    
It is in winform, yes.
I don't use the Linklabel Capacity to contain multiple links. Just simple use.

I suggest you implement Classes, and then bind a ListBox to display links; use serialization to save state.
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Windows.Forms;

namespace WhatEver

    [DataContract]
    public class LinkData // add Properties for Colors
    {
        [DataMember]
        public bool Visited { set; get; }

        [DataMember]
        public string LblText { set; get; }

        [NonSerialized] 
        public LinkLabel LnkLabl;

        public LinkData()
        {
        }

        public LinkData(string lbltext, bool visited) : this()
        {
            LblText = lbltext;
            Visited = visited;
        }
    }

    [DataContract]
    public class LinksData
    {
        public LinksData()
        {
            TheLinks = new List<LinkData>();
        }

        [DataMember]
        public List<LinkData> TheLinks { set; get; }

        public LinkLabel AddLLabel(string text, bool visited)
        {
            LinkData newlink = new LinkData(text, visited);

            LinkLabel newll = new LinkLabel();

            // set colors, etc.
            
            newll.Text = text;

            newll.Links.Add(0, text.Length, text);

            newlink.LnkLabl = newll;

            TheLinks.Add(newlink);

            return newll;
        }
    }
}
You can do something like this to set the LinkLabels in a Panel without manually positioning them:
LinksData lnksdata = new LinksData();

for (int i = 0; i < 10; i++)
{
    LinkLabel llbl = lnksdata.AddLLabel("www.somewhere" + i + ".com", false);

    llbl.LinkClicked  += // your click handler

    panel1.Controls.Add(llbl);

    llbl.Dock = DockStyle.Top;

    llbl.BringToFront();
}
Write the serializer and deserializer, and then you can save the entire state of each LinkLabel in one file. Note that since you cannot serialize the LinkLabel Control itself, you will need to re-create them when you de-serialize.
 
Share this answer
 
I say it's the 'hard way' but maybe it's the normal way. Well, it works for me.
I manage to finish it, after all. But i had to web inside the code for it to work.
C#
List<LinkLabel> ListLinksVisited = new List<LinkLabel>();
void linklabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    if (e.Link.LinkData.ToString() != "")
    {
       // Process.Start(e.Link.LinkData.ToString());
        e.Link.Visited = true; //show in list that link is clicked

        for (int i = 0; i < ListLinksVisited.Count; i++)
        {
            //compare the actual string links
            if (ListLinksVisited[i].Links[0].LinkData.ToString() == e.Link.LinkData.ToString())
            {
                ListLinksVisited[i].LinkVisited = true; //intlink = i;
                break;
            }
        }
    }
}
 
Share this answer
 
v2
Comments
BillWoodruff 17-Aug-19 11:13am    
It does look complex, and I think you can simplify by use of a Dictionary<LinkLabel, bool> cheers, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900