通过获取文件路径来排列TreeView?

发布于 2024-11-18 22:00:01 字数 657 浏览 7 评论 0原文

我有这段代码:

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

如您所见,非常简单,此方法获取文件路径。就像 C:\Windows\notepad.exe

现在我希望 TreeView 像文件系统一样显示它..

-C:\
    +Windows

如果我单击“+”,它会像这样:

-C:\
    -Windows
       notepad.exe

这是我现在通过发送这些论文得到的内容上述方法的路径:

TreeView current Look

我怎样才能做到它会排列节点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晨说晚安 2024-11-25 22:00:01

如果我是你,我会使用 string.Split 将输入字符串拆分为子字符串 方法,然后搜索正确的节点以插入节点的相关部分。我的意思是,在添加节点之前,您应该检查节点 C:\ 及其子节点(Windows)是否存在。

这是我的代码:

...
            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 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;
        }
瑾兮 2024-11-25 22:00:01

如果您使用的是Windows 表单(我猜是这样),您可以实现IComparer 类并使用TreeView.TreeViewNodeSorter 属性:

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

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);
    }
}
动听の歌 2024-11-25 22:00:01

问题是父母和孩子没有区别吗?

树中的每个节点还具有 Nodes 属性,该属性表示其子节点的集合。您的 AddNode 例程需要更改,以便您可以指定要添加子节点的父节点。就像:

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

如果您希望它只是填充路径并找出父子关系本身,您将必须编写一些代码来解析路径,并根据路径段识别父节点。

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.

情释 2024-11-25 22:00:01

尝试看看这个文件系统TreeView。它应该完全符合您的要求。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文