Solved

TTreeview (D2) how dos it work???

Posted on 1998-10-02
9
635 Views
Last Modified: 2008-01-16
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
Comment
Question by:PPaul
  • 6
  • 3
9 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 300 total points
ID: 1341544
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341545
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341546
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:PPaul
ID: 1341547
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341548
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
 

Author Comment

by:PPaul
ID: 1341549
The listbox is because I don't know how to create and destroy objects in delphi....
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341550
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
 

Author Comment

by:PPaul
ID: 1341551
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341552
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question