Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
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
12-Oct-02
Category
Reporting /Printing
Language
Delphi 2.x
Views
43
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

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

Answer:

Lets assume you want to store the memo text into TStringList instances and store 
the stringlist reference into the node.data 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
1   
2   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
3   
4   TStringlist(node.data).Text := 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
5   
6   FLastNode: TTreeNode;


to your form (private section). This field starts out as Nil (no node active).
7   
8   procedure TForm1.tv_eg5Change(Sender: TObject; Node: TTreeNode);
9   begin
10    if Node.Selected and (FLastNode <> Node) then
11    begin
12      if Assigned(FLastNode) then
13        TStringlist(FLastNode.Data).Assign(memo1.lines);
14      Memo1.Lines.Assign(TStringlist(Node.Data));
15      FLastNode := Node;
16    end;
17  end;
18  
19  procedure TForm1.Memo1Exit(Sender: TObject);
20  begin
21    if assigned(FLastnode) then
22      TStringlist(FLastNode.Data).Assign(memo1.lines);
23  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

24  procedure TForm1.Memo1Exit(Sender: TObject);
25  begin
26    if assigned(FLastnode) and not treeview1.focused then
27      TStringlist(FLastNode.Data).Assign(memo1.lines);
28  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
29  
30  TObject(node.data).Free;


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

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC