开发者

Arrange TreeView by getting file paths?

开发者 https://www.devze.com 2023-03-18 22:02 出处:网络
I have this code: public void AddNode(string Node) { try { treeView.Nodes.Add(Node); treeView.Refresh(); } catch (Exception ex)

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 t开发者_运维知识库he 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:

Arrange TreeView by getting file paths?

How can i do that it will arrange the nodes?


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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消