How to delete all sub node in the some treenode?
TTreeNode* ParentNode = TreeView->Selected;
int countNode = ParentNode->Count;
for(int p=0; p<countNode-1; p++)
{
ParentNode->Item[p]->Delete();
}
this code dosn't work!
How to delete all sub node in the some treenode?
TTreeNode* ParentNode = TreeView->Selected;
int countNode = ParentNode->Count;
for(int p=0; p<countNode-1; p++)
{
ParentNode->Item[p]->Delete();
}
this code dosn't work!
Call DeleteChildren
on the parent node:
ParentNode->DeleteChildren();
David gave you the right answer (use TTreeNode::DeleteChildren()
), but you should also understand why your original code was failing so you don't make the same mistake again later.
You were looping forwards from first child to last child, deleting each node as you went along. Every time you deleted a child, the parent node's Count
decremented but you did not update your countNode
variable accordingly, and the index of each following child node decremented but you did not account for that when accessing Item[p]
.
The correct way to write such a loop would look more like this instead:
TTreeNode* ParentNode = TreeView->Selected;
int countNode = ParentNode->Count;
for(int p = 0; p < countNode; ++p)
{
ParentNode->Item[0]->Delete();
}
Or:
TTreeNode* ParentNode = TreeView->Selected;
int countNode = ParentNode->Count;
while (countNode > 0)
{
ParentNode->Item[0]->Delete();
--countNode;
}
Or:
TTreeNode* ParentNode = TreeView->Selected;
int countNode = ParentNode->Count;
for(int p = countNode-1; p >= 0; --p)
{
ParentNode->Item[p]->Delete();
}