Solved

TTreeview (D2) how dos it work???

Posted on 1998-10-02
9
621 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:PPaul
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
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
Comment Utility
The listbox is because I don't know how to create and destroy objects in delphi....
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now