Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 207715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:45:37+00:00 2026-05-11T17:45:37+00:00

I’m new to C# and don’t have any programming experience. But I’ve finish a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:45:38+00:00Added an answer on May 11, 2026 at 5:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.