C# – adding child nodes in treeview

ctreeview

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.

I would like to add a second child for the Second node, I'm quite stuck here and don't know what's next.

Any ideas?

Here is the code:

    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

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?

Best Answer

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.

Related Topic