2012-08-27 55 views
5

すべてのルートノードまたはすべての子ノード(VirtualTreeViewのすべてのノードではありません)を選択します。
私はすべてのルートノードを選択するには、このコードを使用することを試みた:VirtualStringTreeですべてのルートまたはすべての子ノードを選択する方法は?

procedure SelectAllRoots; 
var 
    Node: PVirtualNode; 
begin 
    Form1.VirtualStringTree1.BeginUpdate; 
    Node := Form1.VirtualStringTree1.GetFirst; 
    while True do 
    begin 
    if Node = nil then 
     Break; 
    if not (vsSelected in Node.States) then 
     Node.States := Node.States + [vsSelected]; 
    Node := Form1.VirtualStringTree1.GetNext(Node); 
    end; 
    Form1.VirtualStringTree1.EndUpdate; 
end; 

私は小さなグリッチがあります伝えることができます。 選択が不完全であるか、固まっています。私は間違って何をしていますか?

編集:
私はMultiSelectionを使用しています。

+0

マルチ選択プロパティが有効になっています。それはNode.Statesを使用するように強制します。申し訳ありませんが、これまでに言及していたはずです。 –

答えて

11

1.すべてのルート・ノードを選択します。

すべてのルート・ノードを選択するには、次の手順で使用することができます。

procedure SelectRootNodes(AVirtualTree: TBaseVirtualTree); 
var 
    Node: PVirtualNode; 
begin 
    AVirtualTree.BeginUpdate; 
    try 
    Node := AVirtualTree.GetFirst; 
    while Assigned(Node) do 
    begin 
     AVirtualTree.Selected[Node] := True; 
     Node := AVirtualTree.GetNextSibling(Node); 
    end; 
    finally 
    AVirtualTree.EndUpdate; 
    end; 
end; 

2.すべての子ノードを選択します。

レベルに依存しないすべての子ノードを選択するには、recu

procedure SelectChildNodes(AVirtualTree: TBaseVirtualTree); 
var 
    Node: PVirtualNode; 

    procedure SelectSubNodes(ANode: PVirtualNode); 
    var 
    SubNode: PVirtualNode; 
    begin 
    SubNode := AVirtualTree.GetFirstChild(ANode); 
    while Assigned(SubNode) do 
    begin 
     SelectSubNodes(SubNode); 
     AVirtualTree.Selected[SubNode] := True; 
     SubNode := AVirtualTree.GetNextSibling(SubNode); 
    end; 
    end; 

begin 
    AVirtualTree.BeginUpdate; 
    try 
    Node := AVirtualTree.GetFirst; 
    while Assigned(Node) do 
    begin 
     SelectSubNodes(Node); 
     Node := AVirtualTree.GetNextSibling(Node); 
    end; 
    finally 
    AVirtualTree.EndUpdate; 
    end; 
end; 
+1

魅力のように動作します。ありがとうございました :) –

関連する問題