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 use an item ID in a TTreeView as a unique number 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: Tomas Rutkauskas

Is the ItemID a unique number for a node on the tree at hand only? Apparently this 
ID is not a true handle like for Windows that changes from time to time. I can run 
two instances of the same treeview object and the root node always has a number 
5704360 and the second node always has the 5704400. These item IDs are cast to 
integers. I am trying to create an Outline editor, using a memo and a treeview and 
save the data in the memo to a stream along with some sort of unique identifier in 
order to be able to move nodes around without loosing a nodes assignment to a block 
of memo data. I thought about adding the itemid to the front of the block of data 
and remove it from the data while streaming the data back into the memo. Am I going 
in the wrong direction or what?


I tend towards maintaining my own Ids and TreeNodes in a list:

1   TMemoSource = class(TPersisent)
2   private
3     FId: Integer;
4     FNode: TTreeNode;
5     FStrings: TStringList;
6     function GetStrings: TStrings;
7     procedure SetStrings(Value: TStrings);
8   public
9     property Id: Integer read FId write FId;
10    property Node: TTreeNode read FNode write FNode;
11    property Strings: TStrings read GetStrings write SetStrings;
12  end;
14  TMemoSources = class(TList)
15  private
16    FNextId: Integer; {FNextId needs to be saved/initialised on application close/ 
17  run}
18    function Get(Index: Integer): TMemoSource;
19    procedure Put(Index: Integer; Item: TMemoSource);
20  public
21    function AddItem: TMemoSource;
22    property NextId: Integer read FNextId write FNextId;
23    property Items[Index: Integer]: TMemoSource read Get write Put;
24  end;
26  function TMemoSources.AddItem: TAMemoSource;
27  var
28    Item: TMemoSource;
29  begin
30    Item := TMemoSource.Create;
31    Inc(FNextId);
32    Item.Id := FNextId;
33    inherited Add(Item);
34    Result := Item;
35  end;
37  //I'll let you fill in the other class methods ...
39  //Example of using:
41  procedure TForm1.AddMemoToTree(ANode: TTreeNode; AMemo: TMemo);
42  var
43    Ms: TMemoSource;
44  begin
45    Ms := MemoSources1.AddItem;
46    Ms.Strings.Assign(AMemo.Lines);
47    Ms.Node := TreeView1.Items.AddChildObject(ANode, 'Memo' + IntToStr(Ms.Id), Ms);
48  end;

I find the biggest advantage of maintaining a list is you can hunt the list by Id or Node rather than the treeview. The list approach also lends itself to dynamically adding and deleting nodes to the treeview in the OnExpanding and OnCollapsing events remembering to set Ms.Node := nil if you delete a treenode and don't delete the corresponding memosource.

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