Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding child nodes in treeview

Tags:

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?

like image 660
tintincutes Avatar asked May 19 '09 08:05

tintincutes


People also ask

Which method is used to add child node in TreeView control?

To add nodes programmatically Use the Add method of the tree view's Nodes property.


2 Answers

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.

like image 106
Julien Poulin Avatar answered Sep 28 '22 19:09

Julien Poulin


May i add to Stormenet example some KISS (Keep It Simple & Stupid):

If you already have a treeView or just created an instance of it: Let's populate with some data - Ex. One parent two child's :

            treeView1.Nodes.Add("ParentKey","Parent Text");
            treeView1.Nodes["ParentKey"].Nodes.Add("Child-1 Text");
            treeView1.Nodes["ParentKey"].Nodes.Add("Child-2 Text");

Another Ex. two parent's first have two child's second one child:

            treeView1.Nodes.Add("ParentKey1","Parent-1  Text");
            treeView1.Nodes.Add("ParentKey2","Parent-2 Text");
            treeView1.Nodes["ParentKey1"].Nodes.Add("Child-1 Text");
            treeView1.Nodes["ParentKey1"].Nodes.Add("Child-2 Text");
            treeView1.Nodes["ParentKey2"].Nodes.Add("Child-3 Text");

Take if farther - sub child of child 2:

            treeView1.Nodes.Add("ParentKey1","Parent-1  Text");
            treeView1.Nodes["ParentKey1"].Nodes.Add("Child-1 Text");
            treeView1.Nodes["ParentKey1"].Nodes.Add("ChildKey2","Child-2 Text");
            treeView1.Nodes["ParentKey1"].Nodes["ChildKey2"].Nodes.Add("Child-3 Text");

As you see you can have as many child's and parent's as you want and those can have sub child's of child's and so on.... Hope i help!

like image 45
Roman Polen. Avatar answered Sep 28 '22 18:09

Roman Polen.