Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TTreeNode problem, too few places to store variables

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
Hi all,

Thank you for reading my question! What I would like to do is this: I have 5 variables, 2 integers and 3 strings.
The two integers are width and height parameters and the three strings are a name, text and link parameters. But I cant store this in the nodes themselves. So I was thinking about adding var's to the treenodes. Can this be done, if not how else to go about this, maybe I could put everything in an array.

What I would like to know is how you guys would go about such a thing ???

The reason I'm using a treeview is that I can show my users what their DHTML menu will look like.

Thanks for any input,

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Have you checked out the Delphi Help for TreeView? In Delphi 5 there is a good example of doing exactly what you want.

Look under TTreeNode, Data and take a look at the example code.


Andrew
 
To preview Andrew's advice, you can create an object (e.g. a TCollectionItem descenent) and store it in the .Data property of a TTreeNode.

Cheers
 
You need to create a pointer variable and use the addObject function of the TreeView

Here is a snippet of code I use

TYPE
PmyRec = ^TMyRec;
TMyRec = record
FName: string;
Myint: integer;
end;


VAR
MyRecPtr: PMyRec;
OtherNode, SomeNode, Node : TTreeNode;



Code:
procedure TForm1.FormCreate(Sender: TObject);

begin
   //mount the principal nodes of the Treeview
   with treeview1.Items do
   begin
      
     SomeNode := Add(nil,'first Branch');
     OtherNode:= Add(SomeNode,'second Branch');
       etc...
   end;


Start some iteration process, reading from a database or so
Code:
    New(MyRecPtr);
    MyRecPtr^.FName := 'MyFile';
    MyRecPtr^.MyInt := 3;
    Node := TreeView1.Items.AddObject(SomeNode,'Description 1',MyRecPtr);
   etc....
end;

Here I have bound the data stored in the pointer to a node of the TreeView



Code:
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  if PMyRec(TreeView1.Selected.Data) <> nil then  
  
begin
    StatusBar1.Panels[0].Text:= PMyRec(TreeView1.Selected.Data)^.FName;
    
    Whip1.StreamName:= PMyRec(TreeView1.Selected.Data)^.FName;

    SomeVariable := PMyRec(TreeView1.Selected.Data)^.MyInt;

    end
    else StatusBar1.Panels[0].Text:= 'No File';
end;


In the Onchange Method of the TreeView, I retrieve the data stored to display in a statusbar, use it in ActiveX Control and do some other processing with the integer value stored in the Node

Hope this gives a clue




Steven van Els
SAvanEls@cq-link.sr
 
I did it like this:

first i created a new class like this:

type
TDHTMLTag = class
private
W,H: Integer;
N,C,L: String;
public
property Width: Integer read W write W;
property Height: Integer read H write H;
property Name: String read N write N;
property Caption: String read C write C;
property Link: String read L write L;
end;

then I could just assign it to a variable like you do when working with the registry and fill the properties in the way i want to.

Then i just typecasted it into the Data property like this

var Props: TDHTMLTag;
Props := DHTMLTag.Create;
TreeView1.Selected.Data := Pointer(Props);

Thanks for your help guys,

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
whoops, class ... sheesh, i meant object lol
I like this solution, makes it easy to store and retrieve the information.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Remember that you have to free all these objects.

Making them TCollectionItem descendents is one way.

Cheers
 
I can free them all during the loop, i have to loop through the tree to get all the info. I can free them then.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top