3

我需要根据 C# 中的完整路径扩展我的树视图

我的树视图有 2 个已折叠的节点,我想扩展Node A为数字3 ,所以我有 node 的完整路径A\1\2\3

如何根据完整路径单步执行并打开每个节点?此外,完整路径的长度可能会发生变化,因此我可能需要将节点打开到6 级。所以需要根据全路径来做。任何帮助都会很棒。

Node A
       1
         2
          3
Node B
1
 2
  3
   4 5 6

这是我尝试过的:

TreeNode [] n= treeView1.Nodes.Find(search, true);

if (n.Length > 0)
  found = true;
treeView1.Nodes[t].Collapse();
foreach (TreeNode p in n) {
  string[] a = p.FullPath.Split('\\');
  foreach (string b in a) {
    treeView1.SelectedNode = treeView1.Nodes[b];
    treeView1.SelectedNode.Expand();
4

4 回答 4

3

很抱歉没有评论 S3ddi9 给出的上述答案是正确的。我只是添加一些东西。

所以S3ddi9给出的答案

    ...
    string path = @"A\1\2\";

    var path_list = path.Split('\\').ToList();
    foreach (TreeNode node in treeView1.Nodes)
        if (node.Text == path_list[0])
            ExpandMyLitleBoys(node, path_list);
}

private void ExpandMyLitleBoys(TreeNode node, List<string> path)
{
    path.RemoveAt(0);

    node.Expand();

    if (path.Count == 0)
        return;

    foreach (TreeNode mynode in node.Nodes)
        if (mynode.Text == path[0])
          {  
             ExpandMyLitleBoys(mynode, path); //recursive call
             break;  //this was missing in earlier answer
          } 

}

确实有效,但是您必须添加一个 BREAK (我标记了它),因为如果 for 循环没有完成,return;则不会将您返回到您的 main 函数,并且会抛出异常,因为path[0]is null.

于 2015-01-04T22:59:28.140 回答
1

我希望这会更简单,对于Treeviews,最好的方法是使用递归方法

   ...
        string path = @"A\1\2\";

        var path_list = path.Split('\\').ToList();
        foreach (TreeNode node in treeView1.Nodes)
            if (node.Text == path_list[0])
                ExpandMyLitleBoys(node, path_list);



    }

    private void ExpandMyLitleBoys(TreeNode node, List<string> path)
    {
        path.RemoveAt(0);

        node.Expand();

        if (path.Count == 0)
            return;

        foreach (TreeNode mynode in node.Nodes)
            if (mynode.Text == path[0])
                ExpandMyLitleBoys(mynode, path); //recursive call


    }

这完美地完成了工作

于 2012-09-02T10:47:11.997 回答
0

破解它!

 TreeNode[] n = treeView1.Nodes.Find(search, true);
            if (n.Length > 0)
                found = true;
            treeView1.Nodes[t].Collapse();

            foreach (TreeNode p in n)
            {
                i = 0;
                string[] a = p.FullPath.Split('\\');
                foreach (string b in a)
                {

                    if (i == 0)
                    {

                        treeView1.SelectedNode = treeView1.Nodes[b];
                        current = treeView1.Nodes[b];
                        treeView1.SelectedNode.Expand();
                        i = 1;
                    }
                    else
                    {
                        treeView1.SelectedNode = current.Nodes[b];
                        current = current.Nodes[b];
                        treeView1.SelectedNode.Expand();


                        if (b.ToUpper().Contains(search))
                        {
                            treeView1.SelectedNode.BackColor = System.Drawing.Color.Red;
                        }
于 2012-09-02T10:15:09.077 回答
0

我写了一个更简单的例程,效果很好。根本没有递归...这假设您的路径是完整的文件路径,例如...“C:\ program files \ myapp”并且当您添加节点时,您将节点键设置为等于文件夹名称

string[] strFolders = strPath.Split('\'));
System.Windows.Forms.TreeNode CurrentNode = null;
System.Windows.Forms.TreeNode[] FoundNodes = null;
foreach (string folder in strFolders) {
	if (!folder.Contains(":")) {
		if (CurrentNode == null) {
			FoundNodes = treeFolders.Nodes.Find(folder, false);
		} else {
			FoundNodes = CurrentNode.Nodes.Find(folder, false);
		}
		if (FoundNodes.Length > 0) {
			CurrentNode = FoundNodes[0];
			CurrentNode.Expand();
		} else {
			//no folder found.  cant continue
			break;
		}
	}
}
if (CurrentNode != null) {
	treeFolders.SelectedNode = CurrentNode;
}

于 2015-04-13T15:21:24.993 回答