Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 660
  • Last Modified:

TTreeview (D2) how dos it work???

AddChildFirst, AddChild, AddChildObjectFirst, AddChildObject, AddFirst, Add, AddObjectFirst, AddObject, Insert.and InsertObject

I've started to understand the TTreeview to a point and now came a cross this. AddFirst, Add and Insert I know what it is but  AddChildObjectFirst, AddChildObject, AddObjectFirst, AddObject, and InsertObject I have no clue on what there for and how to use them. Example needed Giving out the points for it.
0
PPaul
Asked:
PPaul
  • 6
  • 3
1 Solution
 
ZifNabCommented:
Hi PPaul,

 for an example look at this address :

http://www.easycash.co.at/delphi/exdbtree.zip
(How to make a simple DBTreeview (like on the net) with the TTreeView).

All the functions related to Object (AddChildObject) etc. give you the ability to assign a record/object to the specified node.

e.g. : Take you want to make a family tree :
 
 - you add all the names into a tree. But heck, now you want to add a picture with them... So use addChildobject to add the names and as object you add the picture. Object can be whatever you want, even your own created objects.
 - now when the user selects a node, you can immediately let the user show the picture assigned to the node.

Regards, Zif.
0
 
ZifNabCommented:
short example as illustration :

TreeView1 := TTreeView.Create;
MyBitmap := TMyBitmap.Create;

{assign values to MyBitmap}

TreeView1.Items.AddChildObject(TreeView1.Selected,'New Item',MyBitMap);

{now you've added a note (named 'New Item', with an attached MyBitmap}

Need more to know? Just ask.

Regards, Zif.

.

{BEFORE DESTROYING TREE, FIRST DESTROY ALL POINTERS = OBJECTS (=here TMyBitmaps MANUALLY!}

TreeView1.Free;


0
 
ZifNabCommented:
Here a short example about nodes, and childnodes ...

again, everything the same for the objectnodes and childobjectnodes, the only difference is that you ADD an object to every node. (don't forget to create memmory for the obect/pointer, AND to remove the memmory when destroying the treeview!)

Put a button and a treeview on your form :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var LastAddedNode, TreeNode2, ChildNode2 : TTreeNode;

begin
 {LastAddedNode contains the last added node }
 LastAddedNode := TreeView1.Items.AddFirst(nil,'Root');
 LastAddedNode := TreeView1.Items.Add(LastAddedNode,'Root');
 TreeNode2 := LastAddedNode;
 LastAddedNode := TreeView1.Items.Add(LastAddedNode,'Root');
 LastAddedNode := TreeView1.Items.Add(LastAddedNode,'Root');
 LastAddedNode := TreeView1.Items.AddChild(LastAddedNode,'Branche1');
 LastAddedNode := TreeView1.Items.AddChild(TreeNode2,'Branche1');
 ChildNode2 := LastAddedNode;
 LastAddedNode := TreeView1.Items.Add(ChildNode2,'Brance1');
 LastAddedNode := TreeView1.Items.AddChild(ChildNode2,'Branche2');
end;

end.


Hope this little example clears some things.

Zif.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PPaulAuthor Commented:
The tree is for a program I'm working on for GURPS Vehicles 2. I need only the Name of the component to show in the list and the data about the component hidden. I was think of using a list box to hold all the data and would like to know if adding an Item that points to a line in the list box would work.... But how to do it????
0
 
ZifNabCommented:
So you want to get this :

A name is put to the tree. And there is data attached to a name? Correct?

Then, put all your data into an own created TObject. Use AddChildObject etc to add. The name and the object :

 LastAddedNode := TreeView1.Items.AddChild(TreeNode2,'NAME',TDataObject);


Why using a TListBox? If a descendant of TObject is possible.

Zif.
0
 
PPaulAuthor Commented:
The listbox is because I don't know how to create and destroy objects in delphi....
0
 
ZifNabCommented:
Hi PPaul,

To create an object (or descendant)

var MyObject : TMyObject;

{create}
  MyObject := TMyObject.Create;

To destroy

 {destroy}
  MyObject.Free;

When working with records & pointers, like Pascal way :

 use GetMem and FreeMem

Regards, Zif.
0
 
PPaulAuthor Commented:
so to creat a string object I would

begin
TDSData:TList;
dsData := TDSData.Create;

{I think thats the Object Class...list or is it listbox???}
dsdata.items.add('Item 1');
dsdata.Items.add('Item 2');
TreeView1.Items.AddChildObject(TreeView1.Selected,'New Item',dsData);

end;

so if I clear and update the DSData Object and then add it to the Treeview each object in the treeview will have it's own seprate data?  And if that's so how do I access that data??
If not and the data changed when I change the TList Object do I need to make a an object for each object in the treeview??

and one last question... when saving the treeview will it save the information of the object in the file????
 
0
 
ZifNabCommented:
Hi PPaul,

Hope this example helps you something more :

Use TStringList to keep your strings
Because you in fact just store a pointer with the treenode, you do have to create every object! (also destroy it yourself!)

you access the data with typecasting & .data property of treenode.

Saving will not save the objects;, I guess never tested it.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    dsData : array[0..1] of TStringList;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
    LastAddedNode, TreeNode2, ChildNode2 : TTreeNode;
begin
 dsData[0] := TStringList.Create;
 dsdata[0].Add('Item 1');
 dsdata[0].Add('Item 2');
 LastAddedNode := TreeView1.Items.AddFirst(nil,'Root');
 TreeView1.Items.AddChildObject(LastAddedNode,'New Item1',dsData[0]);
 dsData[1] := TStringlist.Create;
 dsdata[1].Add( 'Item 3');
 dsdata[1].Add( 'Item 4');
 TreeView1.Items.AddChildObject(LastAddedNode,'New Item2',dsData[1]);
end;

procedure TForm1.TreeView1Click(Sender: TObject);
begin
 if TreeView1.Selected.Data <> nil then begin
  Listbox1.Items.Clear;
  ListBox1.Items.Assign(TStringList(Treeview1.Selected.Data));
 end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var i: integer;
begin
 for i:= 0 to 1 do begin
  dsData[i].Free;
 end;
end;

end.

Regards, Zif.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now