tags:

views:

2948

answers:

3

Hello;-)

I'm new to C# and don't have any programming experience. But I've finish a C# basics. Now I would like to design a simple tree view by adding parent node and child node. And here is a part of the code: I would like to add a second child for the Second node, I'm quite stuck here and don't know what's next. I would appreciate your help here. Thanks;-)

    private void addParentNode_Click(object sender, EventArgs e)
    {
        string yourParentNode;
        yourParentNode = textBox1.Text.Trim();
        treeView2.Nodes.Add(yourParentNode);
    }

    private void addChildNode_Click(object sender, EventArgs e)
    {
        string yourChildNode;
        yourChildNode = textBox1.Text.Trim();
        treeView2.Nodes[0].Nodes.Add(yourChildNode);
    }

Sorry I wasn't clear, I'm not sure if I really need this one here: //treeView1.BeginUpdate(); //treeView1.Nodes.Clear();

What I'm trying to do, is to add Parent Nodes and child node. In my code, I can add several Parent Nodes, but if I want to add a child node, it only add in the first parent node. I want that if I add a child node, I want to add it to the second parent or third parent.

In my code I only use one treeview here which names as treeview2 Here is the screenshot: http://www.mypicx.com/05192009/error/

this is how my final code looks like: Before I put the else, I'm getting an error if I don't select anything. So I made it that way that if there is nothing selected it will add the "child node" to the "default node" or (parent1 node). It seems to work good. Thanks guys;-)

    //This is for adding a parent node
    private void addParentNode_Click(object sender, EventArgs e)
    {
        treeView2.BeginUpdate();

        string yourParentNode;
        yourParentNode = textBox1.Text.Trim();
        treeView2.Nodes.Add(yourParentNode);
        treeView2.EndUpdate();
    }

    //This is for adding child node
    private void addChildNode_Click(object sender, EventArgs e)
    {
        if (treeView2.SelectedNode != null)
        {
            string yourChildNode;
            yourChildNode = textBox1.Text.Trim();

            treeView2.SelectedNode.Nodes.Add(yourChildNode);
            treeView2.ExpandAll();
        }
        //This is for adding the child node to the default node(parent 1 node)
        else
        {
            string yourChildNode;
            yourChildNode = textBox1.Text.Trim();
            treeView2.Nodes[0].Nodes.Add(yourChildNode);
        }

Additional question, Are there any other ways on how the code be better? Because here, I declare the string "yourChildNode" twice. One in the if and other one in the else, are there any simplification? Thanks:-)

+2  A: 

It's not that bad, but you forgot to call treeView2.EndUpdate() in your addParentNode_Click() method.
You can also call treeView2.ExpandAll() at the end of your addChildNode_Click() method to see your child node directly.

private void addParentNode_Click(object sender, EventArgs e) {
  treeView2.BeginUpdate();
  //treeView2.Nodes.Clear();
  string yourParentNode;
  yourParentNode = textBox1.Text.Trim();
  treeView2.Nodes.Add(yourParentNode);
  treeView2.EndUpdate();
}

private void addChildNode_Click(object sender, EventArgs e) {
  if (treeView2.SelectedNode != null) {
    string yourChildNode;
    yourChildNode = textBox1.Text.Trim();
    treeView2.SelectedNode.Nodes.Add(yourChildNode);
    treeView2.ExpandAll();
  }
}

I don't know if it was a mistake or not but there was 2 TreeViews. I changed it to only 1 TreeView...

EDIT: Answer to the additional question:
You can declare the variable holding the child node name outside of the if clause:

private void addChildNode_Click(object sender, EventArgs e) {
  var childNode = textBox1.Text.Trim();
  if (!string.IsNullOrEmpty(childNode)) {
    TreeNode parentNode = treeView2.SelectedNode ?? treeView2.Nodes[0];
    if (parentNode != null) {
      parentNode.Nodes.Add(childNode);
      treeView2.ExpandAll();
    }
  }
}

Note: see http://www.yoda.arachsys.com/csharp/csharp2/nullable.html for info about the ?? operator.

Julien Poulin
thanks Julien, your code works but I would like to add the child node2 in the second parent node. With this code, if I add the childnode2 to the parentnode2 it doesn't do that way. It always add the new child to the parentnode1.
tintincute
Do you want to add a child node to the last inserted node or to the selected node?
Julien Poulin
to the selected node for example.
tintincute
There you go, I edited my answer. Hope I did it correctly this time ;)
Julien Poulin
hey Julien Thanks a lot that's what I need;-) you're an angel;-)
tintincute
When you do this for real :) make sure you check SelectedNode is not null before you try to use it!
Byron Ross
@Byron Ross: Oops, thanks. I'll edit...
Julien Poulin
+1  A: 

Example of adding child nodes:

private void AddExampleNodes()
        {
            TreeNode node;

            node = treeView1.Nodes.Add("Master node");
            node.Nodes.Add("Child node");
            node.Nodes.Add("Child node 2");

            node = treeView1.Nodes.Add("Master node 2");
            node.Nodes.Add("mychild");
            node.Nodes.Add("mychild");
        }
Stormenet
Hi Stormenet, thanks. But I think this one is a finished treeview, is that correct?
tintincute
+1  A: 

It looks like you are only adding children to the first parent treeView2.Nodes[0].Nodes.Add(yourChildNode)
Depending on how you want it to behave, you need to be explicit about the parent node you wish to add the child to.
For Example, from your screenshot, if you wanted to add the child to the second node you would need:
treeView2.Nodes[1].Nodes.Add(yourChildNode)
If you want to add the children to the currently selected node, get the TreeView.SelectedNode and add the children to it.

Try TreeView to get an idea of how the class operates. Unfortunately the msdn documentation is pretty light on the code samples...

I'm missing a whole lot of safety checks here!

Something like (untested):

private void addChildNode_Click(object sender, EventArgs e) {
  TreeNode ParentNode = treeView2.SelectedNode;  // for ease of debugging!
  if (ParentNode != null) {
    ParentNode.Nodes.Add("Name Of Node");
    treeView2.ExpandAll();   // so you can see what's been added              
    treeView2.Invalidate();  // requests a redraw
  }
}
Byron Ross
Thanks Byron, I would like to add the child node to the selected parent node. You mentioned, the TreeView.SelectedNode where can I add it? in the addParentNode or add_ChildNode button?Thanks
tintincute
Hi Byron, in your code "Treenode" is it "TreeNode" or "Treenode". Because if I type "Treenode" I got a red mark. Which means something is wrong.
tintincute