2

I have this code:

    public void AddNode(string Node)
    {
        try
        {
            treeView.Nodes.Add(Node);
            treeView.Refresh();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

Very simple as you see, this method gets file path. like C:\Windows\notepad.exe

Now i want the TreeView to show it like FileSystem..

-C:\
    +Windows

And if i click the '+' it gets like this:

-C:\
    -Windows
       notepad.exe

Here is what i get now from sending theses pathes to the method above:

TreeView current look

How can i do that it will arrange the nodes?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Danpe
  • 16,890
  • 19
  • 89
  • 129
  • #1 I'm sure you own those movies in your example right? #2 Are you adding all the nodes to the root node? #3 http://www.java2s.com/Code/CSharp/GUI-Windows-Form/TreeViewExample.htm – Eric H Jul 06 '11 at 18:36

4 Answers4

1

If I were you, I would split the input string onto substrings, using the string.Split method and then search for the right node to insert the relevant part of a node. I mean, that before adding a node, you should check whether node C:\ and its child node (Windows) exist.

Here is my code:

...
            AddString(@"C:\Windows\Notepad.exe");
            AddString(@"C:\Windows\TestFolder\test.exe");
            AddString(@"C:\Program Files");
            AddString(@"C:\Program Files\Microsoft");
            AddString(@"C:\test.exe");
...

        private void AddString(string name) {
            string[] names = name.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            TreeNode node = null;
            for(int i = 0; i < names.Length; i++) {
                TreeNodeCollection nodes = node == null? treeView1.Nodes: node.Nodes;
                node = FindNode(nodes, names[i]);
                if(node == null)
                    node = nodes.Add(names[i]);
            }
        }

        private TreeNode FindNode(TreeNodeCollection nodes, string p) {
            for(int i = 0; i < nodes.Count; i++)
                if(nodes[i].Text.ToLower(CultureInfo.CurrentCulture) == p.ToLower(CultureInfo.CurrentCulture))
                    return nodes[i];
            return null;
        }
platon
  • 5,140
  • 1
  • 19
  • 24
  • Hey, Thanks! I found these: http://stackoverflow.com/questions/1155977/populate-treeview-from-a-list-of-path http://stackoverflow.com/questions/673931/file-system-treeview/674119#674119 But you gave me the best answer ! – Danpe Jul 06 '11 at 18:57
0

If you are in windows forms (and I guess so), you can implement the IComparer class and use the TreeView.TreeViewNodeSorter property:

public class NodeSorter : IComparer
{
    // Compare the length of the strings, or the strings
    // themselves, if they are the same length.
    public int Compare(object x, object y)
    {
        TreeNode tx = x as TreeNode;
        TreeNode ty = y as TreeNode;

        // Compare the length of the strings, returning the difference.
        if (tx.Text.Length != ty.Text.Length)
            return tx.Text.Length - ty.Text.Length;

        // If they are the same length, call Compare.
        return string.Compare(tx.Text, ty.Text);
    }
}
VMAtm
  • 26,645
  • 17
  • 75
  • 107
0

Is the issue that the parents and children aren't being differentiated?

Each one of the nodes in the tree also has a Nodes property, which represents the collection of its children. Your AddNode routine needs to be changed so you can specify the parent node to whom you want to add a child node. Like:

TreeNode parent = //some node
parent.Nodes.Add(newChildNode);

If you want it to just populate the paths and figure out the parent-child relationships itself, you're going to have to write some code to parse the paths, and identify the parent node based on the path segments.

jwismar
  • 11,772
  • 3
  • 28
  • 42
0

Try taking a look at this Filesystem TreeView. It should do exactly what you are looking for.

Icemanind
  • 43,745
  • 45
  • 159
  • 272