【问题标题】:Populate a (winforms) Treeview recursively以递归方式填充(winforms)Treeview
【发布时间】:2017-08-24 13:05:38
【问题描述】:

我有一个需要动态填充的 TreeView。

内容类似于目录结构(参考附图)。

现在,为了获取这些“文件夹”,我使用了一个命令,该命令将仅列出“顶级”文件夹(参考图片)。 (请注意这不是操作系统目录/文件夹。我只是使用目录/文件夹类比来让事情变得容易理解)

所以,例如我有 Root、Folder1、Sub_Folder1、Sub_Folder2、Sub-sub_folder_1、Folder2,然后发出带有“/”选项的命令会给我一个列表:Folder1、Folder2。

如果我需要 Level-2 文件夹(Sub_Folder_1 和 Sub_Folder_2),我需要再次发出带有选项“/Folder1”的命令..

我需要重复发出这些命令,直到获得最后一个子文件夹并使用列表填充 TreeView。

我正在使用以下 C# (4.5) 代码,但我只能列出 2 个级别。

任何帮助纠正将不胜感激!

try
            {

                BuildInfaCmd(InfaCmdType.ListFolders, folder);

                InfaCmd icmd = CallInfaCmd(InfaCmdExe, InfaCmdArgs);

                if (icmd.ExitCode() == 0)
                {
                    List<string> folders = icmd.GetFolders();

                    if (folders.Count > 0)
                        topFolderFound = true;

                    foreach (string f in folders)
                    {
                        if (node == null) // Add to 'root' of Treeview
                        {
                            TreeNode p = new TreeNode(f);
                            treeView1.Nodes.Add(p);
                            PopulateFoldersRecursive(f, null);
                        }
                        else
                        {
                            callLvl += 1;
                            //MessageBox.Show("Calling recursive " + callLvl.ToString());                            

                            TreeNode p = new TreeNode(f);
                            node.Nodes.Add(p); // Add to calling node as children
                            string fold = node.Text + "/" + f; // The sub-folder to be provided to ListFolder command like -p /RootFolder/SubFolder1/SubFolder2/...
                            PopulateFoldersRecursive(fold, p, callLvl);
                        }
                    }
                }
                else
                {
                    MessageBox.Show(icmd.GetError(), "Error while executing InfaCmd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

【问题讨论】:

标签: c# winforms treeview


【解决方案1】:

提供的答案更具体地用于填充“文件”/“目录”。正如所传达的那样,我的查询中的“文件夹”不是特定于操作系统的,因此答案并没有提供太多帮助。我找到了一种将节点递归添加到 Treeview 的方法。

void PopulateFolders()
        {                  
            int callLvl = 1;
            BuildInfaCmd(InfaCmdType.ListFolders);    

            int timeout = 60000;
            if (lstProjects.SelectedItem.ToString().ToLower().StartsWith("hdp"))
                timeout = 600000;

            InfaCmd icmd = CallInfaCmd(InfaCmdExe, InfaCmdArgs,null,timeout);    

            if (icmd.ExitCode() == 0)
            {
                List<string> folders = icmd.GetFolders();

                foreach (string f in folders)
                {
                    TreeNode p = new TreeNode(f);
                    treeView1.Nodes.Add(p);
                    PopulateFoldersRecursive(f, p, 1);
                }

                lstFolders.DataSource = folders;
            }
            else
            {
                MessageBox.Show(icmd.GetError(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

void PopulateFoldersRecursive(string folder, TreeNode node, [Optional]int callLevel)
        {
            int timeout = 60000;
            if (lstProjects.SelectedItem.ToString().ToLower().StartsWith("hdp"))
                timeout = 600000;
            int callLvl = callLevel;
            string fold = "";                
            try
            {                   

                BuildInfaCmd(InfaCmdType.ListFolders, folder);
                InfaCmd icmd = CallInfaCmd(InfaCmdExe, InfaCmdArgs,null, timeout);     
                if (icmd.ExitCode() == 0)
                {
                    List<string> folders = icmd.GetFolders();                               

                    if (folders.Count > 0)
                        topFolderFound = true;

                    foreach (string f in folders)
                    {                            
                            callLvl += 1;
                            //MessageBox.Show("Calling recursive " + callLvl.ToString());                            

                            TreeNode p = new TreeNode(f);
                            node.Nodes.Add(p); // Add to calling node as children
                                               // MessageBox.Show(callLvl.ToString() + "; Node.text : " + node.Text + " ;  f : " + f);
                            dirTree.Add(p.FullPath);
                            if (String.IsNullOrEmpty(folderFullPath))
                            {
                                //fold = node.Text + "/" + f; // The sub-folder to be provided to ListFolder command like -p /RootFolder/SubFolder1/SubFolder2/...
                                fold = folder + "/" + f; // ORIGINAL                                   
                                folderFullPath = fold;
                            }
                            else
                            {                                   

                                fold = folder + "/" + f; // TEST

                                folderFullPath = fold; // ORIGINAL
                            }

                            PopulateFoldersRecursive(fold, p, callLvl);
                        }
                    }
                }

                else
                {
                    MessageBox.Show(icmd.GetError(), "Error while executing InfaCmd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }    

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException, "Error");
            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2017-02-02
    相关资源
    最近更新 更多