AdvTreeView Nodes Enumeration

HI!

Is it possible to enumerate tree nodes like this:

for(int i = 0; i < AdvTreeView1->GetTotalNodeCount(); i++){
TAdvTreeViewNode *atn = AdvTreeView1->Nodes->Items;
...
...
}

Hi, 


Yes, you can use AdvTreeView1->Nodes->Count

Hello!

1. Is it possible to access a node by unique index? "Index" unique only in current Level.
2. How to to access a node when iterating through a "for"?

Hi,


1) Each node has access to a virtual node, which has a row parameter. The row parameter is unique for each node.
2) You can loop through the nodes with GetParent / FirstChild / NextChild

Hi!


If i already know row parameter, is there possible to access node without GetParent / FirstChild / NextChild?

Hi,


This is undocumented, but you can access the protected method GetNodeForRow via the following code:

type
  TAdvTreeViewOpen = class(TAdvTreeView);

procedure TForm1.FormCreate(Sender: TObject);
begin
  TAdvTreeViewOpen(AdvTreeView1).GetNodeForRow()
end;

Hi!

Found some problems with simple using of TAdvTreeView... Just Adding and removing...

1. Creates three root nodes by AddButton. 
https://imgur.com/lCWZO3G

2. Adding to first root node (Row = 0) one child nodes by selection and AddButton .
https://imgur.com/QuNBNJo

3. Deleting first root node by DelButton.
https://imgur.com/UDsf5aC

4. Trying to add root node by AddButton... But there is "Access Violation..."
https://imgur.com/opb8VpL

What i'm doing wrong?

There is code:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
AdvTreeView1->BeginUpdate();
AdvTreeView1->ClearNodes();
AdvTreeView1->ClearColumns();

AdvTreeView1->Columns->Add()->Text = "1 col";
AdvTreeView1->Columns->Add()->Text = "2 col";
AdvTreeView1->Columns->Add()->Text = "3 col";
AdvTreeView1->EndUpdate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AddButtonClick(TObject *Sender)
{
TAdvTreeViewNode *tn;
if(!AdvTreeView1->FocusedNode){
tn = AdvTreeView1->AddNode();
}
else{
tn = AdvTreeView1->AddNode(AdvTreeView1->FocusedNode);
}
AdvTreeView1->ExpandAll();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DelButtonClick(TObject *Sender)
{
TAdvTreeViewVirtualNode tvn = ((TAdvTreeViewOpen)AdvTreeView1)->GetNodeForRow(0);
if(tvn){
AdvTreeView1->RemoveNode(tvn->Node);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::AdvTreeView1AfterDrawNode(TObject *Sender, TCanvas *ACanvas,
  TRectF &ARect, TAdvTreeViewVirtualNode *ANode)
{
ANode->Node->Text[0] = ANode->Row;
}
//---------------------------------------------------------------------------

Hi,


I have investigated this here but are not able to reproduce this issue.
Yet, I can suspect 2 things.

1) Please do not change or modify properties inside drawing events (The OnAfterDrawNode)
To change the text of the node, please do so at node collection level, or use the OnGetNodeText event
2) The Focused node is not clear properly. When having a focused node, and deleting it could potentially reference an invalid pointer. Please also clear out the focused node, or set the focused node to a different, still existing node.

Thank you!

In Focus was a problem...

Hi!


Q1: How to get the number of levels?
Q2: How to count nodes in current level?
Q3: How to get position in current level for selected node? 
Q4: What the difference between 
AdvTreeView1->GetTotalNodeCount();
and
AdvTreeView1->Nodes->Count; ?

Hi,


Q1: To know the number of levels starting from a specific root node, you can use the following code


  n := AdvTreeView1.GetFirstRootVirtualNode;
  while Assigned(n) do
  begin
    l := n.Level;
    n := n.GetFirstChild;
  end;

Q2: You can retrieve the count of nodes with the virtual node function GetChildCount
Q3: The VirtualNode property provides access to the level, via the Level property
Q4: The GetTotalNodeCount returns the count of all the nodes, and sub nodes
The Nodes.Count property returns only the count of one level of nodes.

Q1: I need levels count for all Tree, Not for specific node.

Q2, Q3, Q4:

I need calculate node name for tree like this

0
--- 4
--- 5
------ 9
--- 6 
1
--- 7
--- 8
2
3

With n.GetNext you can go to the next node and then loop through the child nodes recursively to get to know the level.


Thank you!


Another Q.

Tree before reconstruct:
0
1
2

Tree after first reconstruction:
0
--- 1
--- 2
3
4

Tree after second reconstruction:
0
--- 1
--- 2
3
--- 4
--- 5
6

i have two row_id for reconstruction (for ex. 0 and 1).
But after reconstruction row_id 1 became 3.
Is there some way to detect new row_id after reconstruction? 

Sorry, when adding nodes, or reconstructing, the ID's are being cleared. I suggest you keep a list of your own and then match that list based on the nodes you add. This is typically being done in virtual mode, where you add nodes directly via the OnGetNumberOfNodes instead of the nodes collection.

Hi!

I received a pointer to the node through:
TAdvTreeViewVirtualNode tvn = ((TAdvTreeViewOpen)AdvTreeView1)->GetNodeForRow(MY_ROW_ID);

This is extended node.

Is it possible to set color of the current extended node?

Yes, this can be configured via the NodesAppearance class. There you should be able to configure the appearance of the extended node.

No, no. I know about main configuration of NodesAppearance...

I need to color only one of extended nodes.

Hi,


You can use the OnGetNodeColor event to accomplish this. This event can return a color based on a specific node.