hi,
I'v looped on a treeview and stored all the nodes in List <TreeNode> collection
private void GetNodes(TreeNodeCollection nodes, List<TreeNode> list)
{
foreach (TreeNode node in nodes)
{
list.Add(node);
GetNodes(node.Nodes, list);
}
}
suppose I add
root -- node0 -- node2 -- node5
| |
| |- node3 --node6
|
|- node1 -- node4
the List holds as follows
0 - root
1 - node0
2 - node2
3 - node5
4 - node3
5 - node6
6 - node1
7 - node4
1. I want to save it to disk should I save it using streamwriter
2. I then want to load this file on start of the program what load method should I use
3. then I want to populate the tree and here I'm stuck! I tried some code based on examples from the form:
private void load_Click(object sender, EventArgs e)
{
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
List<TreeNode> roots = new List<TreeNode>();
TreeNode tn = (TreeNode)nodeList[0];
treeView1.Nodes.Add(tn);
roots.Add(tn);
//the roots is used to store the current node and parent node
for (int i = 1; i < nodeList.Count; i++)
{
TreeNode item = nodeList[i];
if (item.Level == roots.Count) roots.Add(roots[roots.Count - 1].LastNode);
AddJob(roots[item.Level],item);
}
treeView1.ExpandAll();
treeView1.EndUpdate();
}
private void AddJob(TreeNode targetNode, TreeNode inputNode)
{
if (targetNode.LastNode == null){
targetNode.Nodes.Insert(targetNode.Index, inputNode);
}
else{
targetNode.Nodes.Add(inputNode);
}
}
and the I get exception that Im tring to inser a node not in the right place what should I do whats wrong here