-1

I have written a nested-loop for adding Tree-Nodes, problem is that index i never increments to 1.

In For Each Loop: where there are no more child nodes in first Parent Node, the loop stops in index j and does not go back to index i to increment to 1.

for (int i = 0; i < 23; i++)
{
    for (int j = 0; j < 23; j++)
    {
        foreach (var item in myDictionaryReconstructed)
        {
            if ("TreeNode: " + item.Key == treeView1.Nodes[i].Nodes[j].ToString())
            {
                treeView1.Nodes[i].Nodes[j].Nodes.Add(item.Value);
                treeView1.ExpandAll();
            }
        } 
    } 
}

For Example:

Parent Node 1 - Sub-Node 1 - Sub-Node 2 - Sub-Node 3

Parent Node 2 - Sub-Node 1 -

Parent Node 3 - Sub-Node 1

The Program executes upto Parent Node - 1 and Sub-Node 3. When the sub-node is 4 being checked and it's not found, the program just stops there, instead of moving to next parent node.

Exception: Specified Argument was out of range of valid values Parameter name index.

Answer to duplicate: after discussing later I found the exception above, my question was posted earlier before I didn't know that it's an index exception.

Fawad Naseer
  • 35
  • 10
  • can you provide some output or something else that can help ? – Zain Arshad Apr 14 '19 at 12:13
  • Something doesn't seem right about this. Are you sure that the inner loops actually complete? – ProgrammingLlama Apr 14 '19 at 12:22
  • 2
    How did you come to that conclusion? During Debug? It would be helpful if you provide a sample structure of the `Tree`, and the items inside `myDictionaryReconstructed`. Also, you sure you don't have an exception of some sort? – Ofir Winegarten Apr 14 '19 at 12:23
  • 1
    Do you *really* have 22 nodes and each have 22 sub-nodes? If not, you will get an exception – Ofir Winegarten Apr 14 '19 at 12:27
  • @OfirWinegarten I have 22 nodes and each node have 2-3 sub nodes. – Fawad Naseer Apr 14 '19 at 13:41
  • @OfirWinegarten I just put the foreach loop of dictionary in try catch block, it freezes interface for 2-3 seconds and completes the loop with correct result now. – Fawad Naseer Apr 14 '19 at 13:49
  • @OfirWinegarten Exception Received: Specified Arguement was out of range of valid values Parameter name index. I don't know how to count the nodes length, should I be counting, parent node length or the sub-node length. – Fawad Naseer Apr 14 '19 at 13:51
  • Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – wimh Apr 14 '19 at 13:59
  • @OfirWinegarten Exception is Thrown after Sub-Node - 3 – Fawad Naseer Apr 14 '19 at 14:04
  • Makes sense, since there's no sub-node 4. – Ofir Winegarten Apr 14 '19 at 14:06

1 Answers1

2

According to our discussion in the comments above,

The following code throws an ArgumentOutOfRangeException

treeView1.Nodes[i].Nodes[j]

When i or j are greater than the number of Nodes that you actually have the exception is raised, as it would be with any other array.

You could do this with foreach or instead of using the magic number (23) use Nodes.Count.

So your code could look like this:

for (int i = 0; i < treeView1.Nodes.Count; i++)
{
    for (int j = 0; j < treeView1.Nodes[i].Nodes.Count; j++)
    {
        foreach (var item in myDictionaryReconstructed)
        {
            if ("TreeNode: " + item.Key == treeView1.Nodes[i].Nodes[j].ToString())
            {
                treeView1.Nodes[i].Nodes[j].Nodes.Add(item.Value);
                treeView1.ExpandAll();
            }
        } 
    } 
}
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27