Solved

TTreeview (D2) how dos it work???

Posted on 1998-10-02
9
644 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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