How to add a child node to a selected parent node programmatically? C++ Winforms

José Carlos 886 Reputation points
2023-04-01T14:02:22.8333333+00:00

Friends.

I'm studying the treeView component and I'm having trouble adding a child node to a selected parent node via programming. The parent node is selected by the checkbox next to the name.

With the line below I add it, but inside the parentheses where there is zero I need to put the selected node and I am not succeeding.

The name of the child node is typed into a textBox2.

treeView1->Nodes[0]->Nodes->Add(textBox2->Text);

Tks.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,621 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,537 questions
{count} votes

Accepted answer
  1. David Lowndes 4,711 Reputation points
    2023-04-02T10:56:10.2233333+00:00

    You should be able to use SelectedNode directly - no need to index into the collection. Something like this:
    treeView1->SelectedNode->Nodes->Add(...)


1 additional answer

Sort by: Most helpful
  1. José Carlos 886 Reputation points
    2023-04-01T19:57:34.92+00:00

    Hi Davi,

    I used this method inside the parentheses instead of zero, by logic that would be it, but it didn't work.

    treeView1->Nodes[treeView1->SelectedNode]->Nodes->Add(textBox2->Text);

    Thank you for the contact.