Author Topic: Treeview from a list of strings?  (Read 2303 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Treeview from a list of strings?
« on: March 26, 2010, 05:37:06 PM »
How to get a List of strings having some identical parts into a treeview in C#?

For instance, if my List of strings looks like so:

Master
Master : Groceries
Master : Groceries : DryGoods
Master : Groceries : DryGoods : Pasta
Master : Groceries : DryGoods : Cereal
Master : Groceries : DryGoods : Flour
Master : Groceries : DryGoods : Sugar
Master : Groceries : Produce
Master : Groceries : Produce : Vegetables
Master : Groceries : Produce : Fruits
Master : Groceries : Produce : Vegetables : Peppers
Master : Groceries : Produce : Vegetables : Carrots
Master : Groceries : Produce : Fruits : Apples
Master : Groceries : Produce : Fruits : Oranges

And I want a tree view to look like so:

Master
  Groceries
     DryGoods
        Pasta
        Cereal
        Flour
        Sugar
     Produce
        Fruits
            Apples
            Oranges
        Vegetables
            Peppers
            Carrots


How do I get from one to the other? I'm drawing a complete blank, possibly due to a much too long week that seems to be getting longer. Thanks for any guidance you can offer up.

Edit: fixed the start of the list to show correctly
« Last Edit: March 26, 2010, 06:16:13 PM by Jeff_M »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Treeview from a list of strings?
« Reply #1 on: March 26, 2010, 05:57:47 PM »
morning Jeff.

How is the data saved/stored ?

Is each line a literal string and each stringset added to a List<Of String> ?

Converting to a dataset and importing into the treeview comes to mind as an idea.
can't play at the moment, but will have a look late this afternoon.


Regards
Kerry [@07:58]

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Treeview from a list of strings?
« Reply #2 on: March 26, 2010, 06:10:54 PM »
Correct, Kerry.
Code: [Select]
                    List<string> matchNames = new List<string>();
                    matchNames.Add("Master : Groceries");
                    //...etc.
There is likely a better method to do what I'm attempting. I actually have 3 Treeviews, with the third one being a comparison between the other 2 and showing the common items. The List of strings was a byproduct of testing, and since I already have it I haven't fully investigated if performing the comparison on the 2 other TreeViews directly is possible.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Treeview from a list of strings?
« Reply #3 on: March 26, 2010, 09:14:17 PM »

I cheated a little :)

Code to follow :
Here's the piccy

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Treeview from a list of strings?
« Reply #4 on: March 26, 2010, 09:20:40 PM »
The code :
slightly different from the piccy ; I moved the List<T> initialisation initialization into the Form1 class.
[edit for the non-queens English :)]
Code: [Select]
//
// Stuff from kwb@theSwamp 2010.03.27

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JeffsTreeview
{
    public partial class Form1 : Form
    {

        List<string> dataList = new List<string>
            {
                @"Master",
                @"Master : Groceries",
                @"Master : Groceries : DryGoods",
                @"Master : Groceries : DryGoods : Pasta",
                @"Master : Groceries : DryGoods : Cereal",
                @"Master : Groceries : DryGoods : Flour",
                @"Master : Groceries : DryGoods : Sugar",
                @"Master : Groceries : Produce",
                @"Master : Groceries : Produce : Vegetables",
                @"Master : Groceries : Produce : Fruits",
                @"Master : Groceries : Produce : Vegetables : Peppers",
                @"Master : Groceries : Produce : Vegetables : Carrots",
                @"Master : Groceries : Produce : Fruits : Apples",
                @"Master : Groceries : Produce : Fruits : Oranges"
            };

        //
        //
        public Form1()
        {
            InitializeComponent();
        }
        //
        //
        private void Form1_Load(object sender, EventArgs e)
        {            
            List<string> massagedList = new List<string>();
            foreach(string item in dataList)
            {
                massagedList.Add(item.Replace(@" : ", @"\"));
            }
            treeView1.PathSeparator = @"\";

            PopulateTreeView(treeView1, massagedList, '\\');
        }
        //
        //
        private static void PopulateTreeView(TreeView treeView, IEnumerable<string> items, char separator)
        {
            TreeNode lastNode = null;
            string subPathAgg;
            foreach(string item in items)
            {
                subPathAgg = string.Empty;
                foreach(string subPath in item.Split(separator))
                {
                    subPathAgg += subPath + separator;
                    TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
                    if(nodes.Length == 0)
                        if(lastNode == null)
                            lastNode = treeView.Nodes.Add(subPathAgg, subPath);
                        else
                            lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
                    else
                        lastNode = nodes[0];
                }

            }
            treeView.ExpandAll();
        }
    }
}
« Last Edit: March 26, 2010, 09:36:38 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Treeview from a list of strings?
« Reply #5 on: March 26, 2010, 10:03:49 PM »
For the record, I coulda read that 'other' English :-)

Thank you very much, Kerry. That's close to what I had started to try before my brain went Kapuut....it makes a lot more sense now that I see it.

Thanks again and have a great rest of your weekend!

Jeff

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Treeview from a list of strings?
« Reply #6 on: March 26, 2010, 10:09:07 PM »
Thanks Jeff.

I'm pleased I could help.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Treeview from a list of strings?
« Reply #7 on: March 27, 2010, 12:56:57 AM »
In case you 'd like to see what I was working on.... this was for the center (centre) Treeview in this pic. Thanks again, with just a few minor adjustments (instead of rebuilding the original list, I just built it to work in the first place earlier in the code) this worked perfectly.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Treeview from a list of strings?
« Reply #8 on: March 27, 2010, 01:41:30 AM »
Very spiffy !

That looks like a big list Jeff, does it populate fast enough ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Treeview from a list of strings?
« Reply #9 on: March 27, 2010, 02:21:35 AM »
Yes it does, Kerry. The slowest part of the whole project is opening the comparison drawing, which is required because there is no way, yet, to access Civil3D objects unless the drawing is open and current. Once the drawing has opened, the data is grabbed, then closed, by the time it finishes closing the form displays. The actual time to populate all 3 treeviews is < 1 second +/- 0.5 second, based on my super accurate eyeball stop watch.

And if you saw the rest of the code you'd be as shocked as I was at how fast it works. I actually use existing code from another part of this project that grabs the Styles and places them into a WPF treeview. I then take THAT data to build my own Lists which I run the comparison on and build the 3 treeviews you see here.  This all started with a "I wonder if this could be done" by someone else, so I used what we already had for other things to test it with. Now that I see how quick it is, I'm going to leave it alone for a while.

This was done with 2 drawings, one with about 1500 C3D styles, the second has almost as many, most of which are the same. This means the matching styles is also around 1500. So it's processing 1500 styles 4 times (twice for the WPF control and twice for the lists for treeviews), then the comparison, then the population of the 3 TreeViews.