Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to synchronize a TTreeView with a TMemo Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius

I'm trying to synchronize different nodes in a TTreeView with different text 
displayed in a TMemo control.


Lets assume you want to store the memo text into TStringList instances and store 
the stringlist reference into the property. The first order of the day is 
to create the stringlists. You do that when you create the nodes, that is the 
logical place to do it. So after you have added a node you do a

node.Data := TStringlist.Create;

The stringlist is initially empty, of course. So if you have some data to load into 
the node you can load it into the stringlist with a statement like

TStringlist( := SomeStringValue;

The act of moving the text to the memo and back is now a simple assignment, no 
stringlists get created for that since we already have them. The main problem is to 
get aware when the user moves to a new node, so the old (perhaps edited) node can 
be updated from the memo. This seems to be another problem that is giving you 
grief. The solution is to track the *last* selected node, the one whos content is 
currently in the memo. Add a field

FLastNode: TTreeNode;

to your form (private section). This field starts out as Nil (no node active).
2   procedure TForm1.tv_eg5Change(Sender: TObject; Node: TTreeNode);
3   begin
4     if Node.Selected and (FLastNode <> Node) then
5     begin
6       if Assigned(FLastNode) then
7         TStringlist(FLastNode.Data).Assign(memo1.lines);
8       Memo1.Lines.Assign(TStringlist(Node.Data));
9       FLastNode := Node;
10    end;
11  end;
13  procedure TForm1.Memo1Exit(Sender: TObject);
14  begin
15    if assigned(FLastnode) then
16      TStringlist(FLastNode.Data).Assign(memo1.lines);
17  end;

You have to check whether the memos Onexit event happens before or after the 
treeviews OnChange event if you have focus on the memo and click on an item in the 
treeview. If it happens after the OnChange event the Onexit handler needs to be 
modified to look like

18  procedure TForm1.Memo1Exit(Sender: TObject);
19  begin
20    if assigned(FLastnode) and not treeview1.focused then
21      TStringlist(FLastNode.Data).Assign(memo1.lines);
22  end;

Otherwise you will assign the memos content just back from the node the OnChange 
handler just loaded it from.

A final thing you have to do is free the stringlist items attached to the treeview 
before the form is destroyed. If you don't use drag & dock with the treeview or the 
form it is on you could use the OnDeletion event of the treeview for this, doing a
24  TObject(;

in the handler. If drag & dock is involved this is not a good idea since the event fires each time the treeview or its parent changes from undocked to docked status and back. In this case the forms OnCloseQuery or OnClose event may be suitable. There you iterate over the treeviews nodes and do the free on each as above.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC