views:

303

answers:

2

i have a very interesting problem.

background

i've used TTreeView for years but only now am using it with a docking library (from AutomatedQA). i've learned that when the parent of the TTreeView is changed, all the handles are recreated. this is something the docking library does during normal activites.

i've been using the Data property on the TTreeNode to hold an object. i've also kept a linkage to the TreeNode in the object. in short, using the TreeNode, i can get the object and i can also get the TreeNode from the object.

as long as the tree view never has it's parent changed (auto-hide & show window), my application works perfectly.

the problem

what i find is that once the parent has changed, my object attached to the tree node's Data property now contains references to the wrong tree node or a stale pointer.

i tried it another way "MyTreeViewContainer.Parent:=SomewhereElse" and observed the same problem.

reloading the tree with possibly hundreds of objects wouldn't be very efficient.

i don't see how i should work around this problem. i have for so long been able to load the tree view and have the objects remain able to find each other easily. this could really break my nice "pattern" for using the tree view...

thank you for your help!

A: 

i think i've found the answer. use the OnAddition event of the TTreeView to reconnect the node & object as follows:

procedure TForm1.TreeView1Addition(Sender: TObject; Node: TTreeNode); begin TMyObject(Node.Data).TreeNode:=Node; end;

X-Ray
+1  A: 

Derive a new component from TTreeView and override the virtual CreateWnd() and DestroyWnd() methods to load/save your custom data from/to temporary storage when needed. Several of the VCL's native components (TTreeView included) do that internally. Changing the Parent property is not the only operation that can cause the TreeView's window handle to be recreated.

Remy Lebeau - TeamB