mvezinet
mvezinet

Reputation: 99

Add a node to TTreeView in C++ Builder

It is a very simple question that I have to ask, but I couldn't find the answer anywhere.

I'm using C++ Builder XE6 and I want to use a TTreeView. I have found several tutorials about it, saying that the method to add a node is to do this :

TreeView->Items->Add(NULL, "name");

But it doesn't work, I get the error that Add() is not a member of Items. After a quick research, I have found that Add() is a method for TTreeNodes, but TreeView->Items is a TTreeViewItem. Maybe all the tutorials that I have read are outdated. Anyway, I can't find any way to do it.

Thank you for your help.

Upvotes: 0

Views: 2716

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595971

TTreeViewItem is a FireMonkey class, not a VCL class. All of the tutorials you have read are likely based on VCL.

In VCL, TTreeView::Items as a TTreeNodes object:

__property TTreeNodes* Items = {read=FTreeNodes, write=SetTreeNodes};

TTreeNodes does have an Add() method:

TTreeNode* __fastcall Add(TTreeNode* Sibling, const System::String S);

The code you showed works fine in VCL.

In FireMonkey, TTreeView::Items is an indexed array of TTreeViewItem objects:

__property TTreeViewItem* Items[int Index] = {read=GetTreeItem};

TTreeViewItem does not have an Add() method. The correct way to add a new node to a FireMonkey TTreeView is to create a TTreeViewItem object and set its Parent property, eg:

TTreeViewItem *node = new TTreeViewItem(TreeView);
node->Text = "name";
node->Parent = TreeView;

Upvotes: 1

Rodrigo Gómez
Rodrigo Gómez

Reputation: 1079

You need to call TreeView->Items->AddChild(NULL, "name"); - This will add a child node of root (NULL). If you need to add a child of a specific node then you need to pass the node as parameter.

According to the docs, and a quick check in the hpp file, Items is a TTreeNodes so Add and AddChild should work. Are you sure you're not accessing, for instance, Items[0] ?

Upvotes: 0

Related Questions