Board index » cppbuilder » Moving nodes

Moving nodes


2007-11-18 06:06:12 AM
cppbuilder66
How come when I select more than one node, and I drop the
nodes somewhere else, only one of the selected nodes get moved?
TTreeNode *SourceNode = TreeView1->Selected;
TTreeNode *TargetNode = TreeView1->DropTarget;
if((TargetNode) && (SourceNode != TargetNode))
{
for(int x = 0; x < TreeView1->SelectionCount; ++x)
{
SourceNode = TreeView1->Selections[x];
SourceNode->MoveTo(TargetNode, naAddChild);
}
}
 
 

Re:Moving nodes

Hi CJ
CJ says:
Quote
How come when I select more than one node, and I drop the
nodes somewhere else, only one of the selected nodes get moved?

TTreeNode *SourceNode = TreeView1->Selected;
TTreeNode *TargetNode = TreeView1->DropTarget;

if((TargetNode) && (SourceNode != TargetNode))
{
for(int x = 0; x < TreeView1->SelectionCount; ++x)
{
SourceNode = TreeView1->Selections[x];
SourceNode->MoveTo(TargetNode, naAddChild);
}
}
I dont Know, but a couple of ideas:
Have You checked the value of TreeView1->SelectionCount
for each loop ?
are You sorounding Your code by
TreeView1->BeginUpdate();
and
TreeView1->EndUpdate();
Just what I would try to begin with.
Kind regards
Asger
 

Re:Moving nodes

"CJ" < XXXX@XXXXX.COM >wrote in message
Quote
How come when I select more than one node, and
I drop the nodes somewhere else, only one of the
selected nodes get moved?
You are not taking into account that moving selected nodes around changes
the TreeView's selection status. After you move the first node, the
SelectionCount may be decremented, causing your loop to exit prematurely.
To do what you are attempting, you should store the desired nodes into a
separate list first, and then loop through that list separately. For
example:
TTreeNode *SourceNode = TreeView1->Selected;
TTreeNode *TargetNode = TreeView1->DropTarget;
if( (TargetNode) && (SourceNode != TargetNode) )
{
TList *TmpList = new TList;
try
{
for(int x = 0; x < TreeView1->SelectionCount; ++x)
TmpList->Add(TreeView1->Selections[x]);
for(int x = 0; x < TmpList->Count; ++x)
{
SourceNode = (TTreeNode*) TmpList->Items[x];
SourceNode->MoveTo(TargetNode, naAddChild);
}
}
__finally
{
delete TmpList;
}
}
Gambit
 

{smallsort}

Re:Moving nodes

Quote
You are not taking into account that moving selected nodes around changes
the TreeView's selection status. After you move the first node, the
SelectionCount may be decremented, causing your loop to exit prematurely.
Thanks, that was exactly my problem. The loop did exit prematurely and it
was confusing me.
Thanks again