How to update Node's index after the MoveTo Function performed in TcxTreeList of Delphi

882 views Asked by At

What I want to achieve is that users can re-order nodes by clicking 'up' and 'down' button. Here's my code.

 procedure TForm1.btnUPClick(Sender: TObject);
 var tempNode : TcxTreeListNode;
 sIndex : integer;
 begin
    if cxTreeList1.FocusedNode = nil then exit;
    cxTreeList1.FocusedNode.MoveTo(cxTreeList1.FocusedNode.GetPrev, tlamInsert);
 end;       

It is no wonder that the code is working fine. I can see nodes are moving up and down. but it seems like their index doesn't updated after the move.

test code:

for i:=0 to cxTreeList1.count -1 do begin
   showmessage(cxTreeList1.nodes[i].values[0]);
end;

result: apple, banana, candy, diamond, elephant

after click 'up' and 'down button more than a hundred time.

result: apple, banana, candy, diamond, elephant

What I expected

elephant, banana, candy, diamond, apple (because I re-ordered nodes)

1

There are 1 answers

1
MartynA On BEST ANSWER

You're referring to the Developer Express TcxTreelist, yes?

The reason I ask is that if if I try your code, the compiler objects to "Nodes" as an unknown identifier, so I'm not clear how you're managing to get your code to compile, let alone run. However, with Devex's TcxTreelist, the following code correctly lists the tree items in the order they are currently displayed (i.e. what I see in the treelist after I've clicked the Up button on, say 'Banana'), not their original order as displayed in the Items editor in the IDE. Note that this uses the treelist's Items property, not its "Nodes":

procedure TForm1.btnGetItemsClick(Sender: TObject);
var
  i : Integer;
begin
  Memo1.Lines.Clear;
  for i:=0 to cxTreeList1.Count - 1 do begin
     Memo1.Lines.Add(cxTreeList1.Items[i].Values[0]);
  end;
end;

Btw, regarding the point in your q's title: Your assumption that the node's Index property needs updating is wrong, I think.