Thread: Programmatically creating nodes in a treeview problem

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    129

    Programmatically creating nodes in a treeview problem

    I'm reading in a csv file, to create an array called 'shipsList[x]', and then I want to use this information to both create a treeview node structure, and also fill another array. The information read in to 'shipsList[x]' is:

    Code:
    X,parentNodeName,Y,childNodeName,Z,grandchildNodeName,val1,val2,val3,val4
    Under each parent node, there are any number of child nodes, the same with the grandchild nodes under the childs. Basically, I am trying to read the 'shipsList[x]' array, and get the program to read the first value, decide if it's X, Y, or Z, and for the first two, read the next value in 'shipsList[x]', add a new node with this name, and move along. For Z, add the next item, and ignore the rest (or, add that data to another array), and move along again, until it runs out of items. However, my code below, successfully reads the parent nodes, but throws all the child nodes under the very first created parent node, as i cannot figure out how to add a child node to the most recently created parent node, and the same for the grandchild under child nodes. Heck, I haven't been able to get anywhere with the grandchildren. Got any ideas?

    Code:
                treeView1.BeginUpdate();
                treeView1.Nodes.Clear();
                int counter = 0;
                for (counter = 0; counter <= 500; counter++)
                {
                    switch (shipsList[counter])
                    {
                        case "X":
                            //want to create a top node and name it
                            treeView1.Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter += 1;
                            break;
                        case "Y":
                            //want to create a child node under the above top node (in 'case 0:') and name it
                            treeView1.Nodes[0].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter += 1;
                            //treeView1.Nodes[nodeNumber].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            break;
                        case "Z":
                            //want to create a grandchild node under 'case 1:' and name it, then also
                            //add the attributes to shipsDataGroup
                            //treeView1.Nodes[2].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter += 0;
                            break;
                    }
                }
                treeView1.EndUpdate();
    P.S. This really is not homework, this is a personal project.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    ok, I can get the child nodes added -

    Code:
    treeView1.BeginUpdate();
                treeView1.Nodes.Clear();
                //int nodeNumber = 0;
                int counter = 0;
                int parentNodeCounter = -1;
                int childNodeCounter = -1;
                for (counter = 0; counter <= 500; counter++)
                {
                    switch (shipsList[counter])
                    {
                        case "X":
                            //want to create a top node and name it
                            treeView1.Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter++;
                            parentNodeCounter++;
                            break;
                        case "Y":
                            //want to create a child node under the above top node (in 'case 0:') and name it
                            treeView1.Nodes[parentNodeCounter].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter++;
                            childNodeCounter++;
                            //treeView1.Nodes[nodeNumber].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            break;
                        case "Z":
                            //want to create a grandchild node under 'case 1:' and name it, then also
                            //add the attributes to shipsDataGroup
                            //treeView1.Nodes[childNodeCounter].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter++;
                            break;
                    }
                }
                treeView1.EndUpdate();
    But I still have no idea to get round the grandchild nodes...

    'Specified argument was out of the range of valid values.
    Parameter name: index'

    that refers to:

    Code:
    //treeView1.Nodes[childNodeCounter].Nodes.Add(new TreeNode(shipsList[counter + 1]));
    and it's childNodeCounter value. I cannot get this node type added! Help!
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    I've done it, finally.

    Code:
    treeView1.BeginUpdate();
                treeView1.Nodes.Clear();
                //int nodeNumber = 0;
                int counter = 0;
                int parentNodeCounter = -1;
                int childNodeCounter = -1;
                for (counter = 0; counter <= 500; counter++)
                {
                    switch (shipsList[counter])
                    {
                        case "X":
                            //want to create a top node and name it
                            treeView1.Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter++;
                            parentNodeCounter++;
                            childNodeCounter = -1;
                            break;
                        case "Y":
                            //want to create a child node under the above top node (in 'case 0:') and name it
                            treeView1.Nodes[parentNodeCounter].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter++;
                            childNodeCounter++;
                            //treeView1.Nodes[nodeNumber].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            break;
                        case "Z":
                            //want to create a grandchild node under 'case 1:' and name it, then also
                            //add the attributes to shipsDataGroup
                            treeView1.Nodes[parentNodeCounter].Nodes[childNodeCounter].Nodes.Add(new TreeNode(shipsList[counter + 1]));
                            counter++;
                            break;
                    }
                }
                treeView1.EndUpdate();
    As you can see, i had to reference the parent, and the child, and it now works!
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating three nodes, deleting head
    By Nakeerb in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 09:00 PM
  2. Problem creating .exe files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-22-2002, 02:25 PM
  3. Problem of Creating Two Constructors in a Class
    By Yin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 09:17 AM
  4. Replies: 3
    Last Post: 12-03-2001, 01:45 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM