TreeView Iteration

coderguy1985

New member
Joined
Apr 3, 2015
Messages
2
Programming Experience
3-5
I'm trying to build a tree view off this old menu system and need to figure out the best way of doing this.

Basically I will read in the MAIN.TXT file below and those will be my high level nodes, but sub menus exist in MAIN.TXT like Submenu.txt and SecondSubmenu.txt, those will be separate text files.

Now my best guess here is that I should load in MAIN.txt first, then iterate through that list, and then iterate through the sub menus. The problem I am having is how would I structure my iteration to perform this?

Sample text files:

MAIN.TXT
A|runme.bat|Main Execution
A|runthis.bat|Secondary Execution
A|Submenu.txt|SubMenu
A|runagain.bat|Tietary Execution
A|SecondSubmenu.txt|SecondSubMenu

SUBMENU.TXT
A|Test.exe|Test Executable

SECONDSUBMENU.TXT
A|Secondmenu.bat|Second Menu batch
A|Thirdmenu.txt|ThirdSubMenu

THIRDMENU.TXT
A|Thirdmenu.bat|Third Menu batch



What I want my output to be:


C#:
runme.bat - main execution
runthis.bat - secondardy execution
Submenu.cfg - SubMenu
     Test.exe - Test Executable
runagain.bat - tietary execution
secondsubmenu
     Secondmenu.bat - Second Menu batch
    Thirdmenu.txt - thirdSubMenu
     thirdmenu.bat - Third Menu batch
 
Last edited:
This is obviously not a general question. A TreeView is a control so the question is specific to the UI technology in use. Is it a Windows Forms TreeView or WPF or Web Forms? Once I know that, I can move this thread to the proper forum and provide a technology-appropriate response.
 
Any tree structure is inherently recursive so it's almost always preferable to use recursion to traverse a TreeView. In the case of the WinForms TreeView, the control itself has a Nodes property that is type TreeNodeCollection and so does every TreeNode, so that is the place to recurse from. In your case, you'll want a method something like this:
private TreeNode[] PopulateNodesFromFile(TreeNodeCollection nodes, string filePath)
{
    var childNodes = new List<TreeNode>();

    foreach (var line in File.ReadLines(filePath))
    {
        var node = new TreeNode();

        // Configure node based on line.

        // If line contains child file name, recurse to populate child nodes.
        PopulateNodesFromFile(node.Nodes, "child file path here");

        childNodes.Add(node);
    }

    nodes.AddRange(childNodes.ToArray());
}
You'll start the ball rolling by calling that method and passing the TreeView's own Nodes collection and the path of the main file.
 
Back
Top Bottom