有代码中的某个时刻,不清楚:
- 你想明确地限制路径深度(例如显示路径可达3D级别只)
- 什么是ListWithPaths?两条路径可以有一个公共节点吗?
,以显示与限制(深度)的单一路径的代码可能是
private void PopulateTree(String path, TreeNode parent, int depth) {
if (depth == 0) // <- Artificial criterium
return;
if (String.IsNullOrEmpty(path))
return;
int index = path.IndexOf(Path.DirectorySeparatorChar);
String directoryName = (index < 0) ? path : path.Substring(0, index);
String otherName = (index < 0) ? null : path.Substring(index + 1);
TreeNode childNode = parent.Nodes.Add(directoryName);
PopulateTree(otherName, childNode, depth - 1);
}
为了加载的路径的集合,没有任何限制有可能的共同节点可以使用 像这个:
private void PopulateTree(String path, TreeView view, TreeNode parent) {
if (String.IsNullOrEmpty(path))
return;
int index = path.IndexOf(Path.DirectorySeparatorChar);
String directoryName = (index < 0) ? path : path.Substring(0, index);
String otherName = (index < 0) ? null : path.Substring(index + 1);
TreeNode childNode = null;
TreeNodeCollection nodes = (parent == null) ? view.Nodes : parent.Nodes;
foreach (TreeNode node in nodes)
if (String.Equals(node.Name, directoryName)) {
childNode = node;
break;
}
if (childNode == null)
childNode = nodes.Add(directoryName);
PopulateTree(otherName, view, childNode);
}
private void PopulateTree(IEnumerable<String> paths, TreeView view) {
view.BeginUpdate();
try {
foreach (String path in paths)
PopulateTree(path, view, null);
}
finally {
view.EndUpdate();
}
}
...
PopulateTree(ListWithPaths, MyTreeView)
它只会产生一个,因为你不断制作父节点,只有当你点击零时才生小孩。你用深度来表示你想要一个子节点吗? – Kixoka
我需要类似这样的内容我更新了我的问题 – Jake
尝试重构代码以使2个方法之一添加一个父项并添加一个子项....您可能需要提前确定构成父项的内容并创建您的集合相应地,然后旋转收集并相应填充。老实说,有很多的例子,如果你谷歌它... – Kixoka