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.

Developer technologies | C++
Developer technologies | 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.
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
{count} votes

Answer accepted by question author
  1. David Lowndes 4,726 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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.