Question

I want to write an application to parse shortlinks in a treeview in order of hostnames like:

Input:
bit.ly/sh125ad
adf.ly/sh1d5ad
bit.ly/sh1d5ad
adf.ly/sh125ad
adf.ly/sh12had

Output:
adf.ly
    adf.ly/sh1d5ad
    adf.ly/sh125ad
    adf.ly/sh12had
bit.ly
    bit.ly/sh125ad
    bit.ly/sh1d5ad

After I wrote the parsing part:

List<string> links = linkField.Text.Split('\n').ToList();

I don't know how I should add the parent nodes in treeView.Nodes and then add the Child ones. My try was:

foreach (string link in links)
{
    Uri url = new Uri(link);
    if (!treeView.Nodes.Contains(new TreeNode(url.Host)))
         treeView.Nodes.Add(new TreeNode(url.Host));
}

But if I wanted to add the links I couldn't find the first TreeNode, because of its childs

Was it helpful?

Solution

This is a bit messy way but its quickly does what you want. First of all make a class to hold each uniqe domains and its URLs:

public class ShortLink
{
    public string DomainName { get; set; }
    public List<string> UrlList;

    public ShortLink(string domainName)
    {
        DomainName = domainName;
        UrlList = new List<string>();
    }

    public void AddUrl(string url)
    {
        if(!UrlList.Contains(url))
            UrlList.Add(url);
    }

    public string GetFullUrl(int indexOfUrlInList)
    {
        return DomainName + "/" + UrlList[indexOfUrlInList];
    }

    //Add other methods like remove and stuff
}

Assuming that your form has a treview called treeView1 then try the code below:

public partial class Form1 : Form
{
    private List<string> _links = new List<string>(new []{
        "bit.ly/sh125ad",
        "adf.ly/sh1d5ad",
        "bit.ly/sh1d5ad",
        "adf.ly/sh125ad",
        "adf.ly/sh12had"
    });

    private List<ShortLink> _shortLinks = new List<ShortLink>();

    public Form1()
    {
        InitializeComponent();

        PopulateTreeView();
    }

    private void PopulateTreeView()
    {
        //Make the dictionary
        foreach (var link in _links)
        {
            var parts = link.Split('/');
            var domainPart = parts[0];

            if (!_shortLinks.Contains(_shortLinks.FirstOrDefault(x => x.DomainName == domainPart)))
            {
                var shortLink = new ShortLink(domainPart);
                _shortLinks.Add(shortLink);
            }
        }

        foreach (var link in _links)
        {
            var parts = link.Split('/');
            var domainPart = parts[0];
            var urlPart = parts[1];
            //find a ShortLink in the list we made that matches this domainpart
            var shortLink = _shortLinks.FirstOrDefault(x => x.DomainName == domainPart);

            //if its not null, we add the url part to taht!
            if(shortLink != null)
                shortLink.AddUrl(urlPart);

            //now we have all domains and parts
        }


        //Populate treeview
        foreach (var shortLink in _shortLinks)
        {
            var parentNode = new TreeNode(shortLink.DomainName);
            var childNodes = new List<TreeNode>();

            //add all the links in the current shortLink to the list of treenodes
            foreach (var link in shortLink.UrlList)
            {
                childNodes.Add(new TreeNode(link));
            }

            //now time to add child notes to parent node
            parentNode.Nodes.AddRange(childNodes.ToArray());

            treeView1.Nodes.Add(parentNode);
        }
    }
}

UPDATE

I just realised your question was actually about finding parent nodes. See the method below:

private void AddToTreeView(string domain, string link)
{
    foreach (TreeNode node in treeView1.Nodes)
    {
        if (node.Text == domain)
        {
            node.Nodes.Add(link);
        }
    }
}

Of course you can create a full node details when creating Node in a foreach loop. You can set fieds such as Name and Tag for each node for further use!

You can also subclass the TreeNode and have your own custom fields to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top