通过获取文件路径来排列TreeView?
我有这段代码:
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
这是我现在通过发送这些论文得到的内容上述方法的路径:
我怎样才能做到它会排列节点?
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:
How can i do that it will arrange the nodes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我是你,我会使用 string.Split 将输入字符串拆分为子字符串 方法,然后搜索正确的节点以插入节点的相关部分。我的意思是,在添加节点之前,您应该检查节点 C:\ 及其子节点(Windows)是否存在。
这是我的代码:
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:
如果您使用的是Windows 表单(我猜是这样),您可以实现
IComparer
类并使用TreeView.TreeViewNodeSorter 属性:If you are in windows forms (and I guess so), you can implement the
IComparer
class and use the TreeView.TreeViewNodeSorter property:问题是父母和孩子没有区别吗?
树中的每个节点还具有 Nodes 属性,该属性表示其子节点的集合。您的 AddNode 例程需要更改,以便您可以指定要添加子节点的父节点。就像:
如果您希望它只是填充路径并找出父子关系本身,您将必须编写一些代码来解析路径,并根据路径段识别父节点。
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:
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.
尝试看看这个文件系统TreeView。它应该完全符合您的要求。
Try taking a look at this Filesystem TreeView. It should do exactly what you are looking for.