0

I have a custom structure which holds Nodes info(level,index,childCount), and the struct is stored in sequence in a binary file. Now, I need to rebuild the TreeView from the file, I got the structure[] back, but stuck in converting the structure to the nodes. Here is a image,the left part of the image represents the struct array, same as the binary file structure. The right part is the TreeView I want to build. enter image description here

I only hope someone can give me a tip about the recursive function, since I'm still learning C# :) THANKS.

Awd
  • 11
  • 2
  • you need to include some code to show us where you got stuck – derloopkat Apr 08 '18 at 13:05
  • TreeNode is serializable, why do you need a custom structure to _hold_ its info? – TnTinMn Apr 08 '18 at 13:25
  • @TnTinMn The custom structure which is from the binary file, is not my work, I'm kind of working on cracking the file structure. – Awd Apr 09 '18 at 01:07
  • you can try this link... https://stackoverflow.com/questions/49702815/winforms-treeview-with-checked-nodes-recursion/49707471#49707471 – Aakash Singh Apr 09 '18 at 07:33

2 Answers2

0

I think you can use Name field and Find function. Name is a key and Find function find a nodes by Name Field. at first, you need to save a unique key value each nodes. and then you will find a node by key and make a hierarchy

Here is sudo code,

Treeview myTreeView = new Treeview();

TreeNode parentNode = new TreeNode();  // maybe it will index = 1 , level = 0 in your image
parentNode.Name = "uniquevaule";
myTreeView.nodes.add(parentNode);

TreeNode childNode = new TreeNode(); // maybe it will index = 0 , level = 1
childNode.Name = "child";
TreeNode[] nodes = myTreeView.Nodes.Find("uniquevaule", true);
nodes[0].add(childNode);
BKing
  • 51
  • 4
0

The function i've made assumes that the leveling always begin at Level 0 based on the image you've provided.

This function also assumes that after a Level 0 item, any value of level, greater than 0 will be considered as a child node of the previously encountered level 0 item.

What i did here is that, i've created a list of TreeNodes and a list of Levels based on your struct, and will loop through the list based on levels, if a level is detected greater than the initially provided level, a recursive call will be made to process the sub levels.

 public List<TreeNode> MainNodes(List<Levels> strut, int level){

    List<TreeNode> parentNode = new List<TreeNode>();
    for(int x = 0; x< strut.Count; x++){
        var data = strut[x];
        if(data.Level == level){
            TreeNode nodeC = new TreeNode();
            nodeC.Name = strut[x].Index.ToString(); //Index value from struct
            if(x + 1 < strut.Count){ 
                if(strut[x+1].Level > level){
                    var newArray = new List<Levels>(strut);
                    newArray.RemoveAt(x);
                    nodeC.Nodes.AddRange(MainNodes(newArray, level + 1);
                }
            }
            parentNode.Add(nodeC);
        }
    }
    return parentNode;
}

enter image description here This functions returns the nodes and sub-nodes based on your requirement.

Anjo
  • 161
  • 6
  • Thanks, this worked, but there seems to be a error at first. The Nodes.AddRange() needs TreeNode[] nodes not list, so I change it to nodeC.Nodes.AddRange(MainNodes(newArray, level + 1).ToArray()) – Awd Apr 13 '18 at 03:04
  • No worries, just used list for testing, forgot to change it back to array since it was the one you've used, but all ends well :D – Anjo Apr 13 '18 at 11:37