I need help on saving individual data to a TTreeNode-Component.
The definition is as follows:
function AddObject(Node: TTreeNode; const S: string; Ptr: Pointer):
TTreeNode;
My problem is to find an object, that I can setup for the Ptr variable. Is
it possible to
use a structured type or something like that to save string and integer
information to
each TreeNode or (best) a whole DB-Table-Record. I need to save
KeyField-Information.
Many thanks,
Alexander Achenbach
It's quite easy - I do this frequently. You can store either a pointer to
structure or an object. I usually create a small object that contains handy
stuff about the database row:
Type MyDbInfo=class
DbID: integer;
DbDesc: string;
public
property DbID: integer read FDbID write FDbID;
property DbDesc: string read FDbDesc write FDbDesc;
end;
If this object is to referenced throughout your form, declare it in the
Private section:
FMyDbInfo: TMyDbInfo;
Then when you build your treeview, create a new instance of the object for
every node and store it in the node:
FMyDbInfo := TMyDbInfo.Create;
FMyDbInfo.DbId := Query1.FieldByName('id').AsInteger;
FMyDbInfo.DbDesc := Query1.FieldByName('desc').AsString;
AddChildObject(aParentNode,FMyDbInfo.DbDesc,FMyDbInfo);
DO NOT FORGET to free them when finished:
for i := 0 to TtreeView1.Items.Count - 1 do begin
if assigned(TreeView1.Items[i].Data) then
TMyDbInfo(TreeView1.Items[i].Data).Free;
end;
Hope this helps.
Oops. I forgot to mention how to reference the object!. Either cast the
selected row (or use items[] property) or use the AS operator:
var1 := TMyDbInbo(TreeView1.Selected.Data).DbID;
Good luck!
By the way, do you know how to save entire TreeView (or TreeNode) and
its property to a file (like Expanded or which is Selected)? And next
time loading it from saved file.
I suggest using objects rather than records, just because object operators are
more convenient, and by being objects you have the ability to add methods if
you need. Using objects makes your code more familiar to the typical Delphi
programmer. For exampl,e Create and Free are more familiar than New and
Dispose, and using objects means you never have to bother with the syntax of
typed pointers and pointer references.
If you want the nodes to store result set information you can either create an
object that has properties corresponding to fields on your database record,
or, if you can leave the query active and there aren't too many records in the
result set, you could simply create a bookmark for each row and assign the
bookmark reference to the each tree node's Data property. From a node you'd
get information about the record by type-casting the Data property back to a
bookmark, resetting currency to the bookmark, and referencing the record's
fields as usual. (If data-aware controls are linked to the result set this may
not work well because when you go to the bookmark you will change currency,
which will be reflected by the data-aware controls.) Bookmarks become invalid
when a table closes, and resourses allocated for bookmarks have to be managed
(e.g., using FreeBookmark), so even though this may be a quicker solution it
is error-prone. With the object solution you would create a class which
includes the properties you are interested in, and when you open your dataset
you would iterate through the result set. For each record you would create an
object, set its properties to values from the record, and assign the object
reference to a node's Data property. Remember to eventually free the objects.
I'm not sure what the AddObject function is supposed to do. If you already
have a pointer to something you can simply assign it to the node's Data
property -- you don't need a function for that.
If you could give more details about what you're trying to do I could give
better examples. I hope this helps...
Alexander Achenbach wrote:
Alexander Achenbach wrote:
--
Catalyst Consulting Corp. http://www.ObjectSamurai.com (608) 345-6767