Link to home
Start Free TrialLog in
Avatar of tzigi
tzigi

asked on

Adding a new property (String) to a TreeNode.

Greetings,
 How can I add a new property to a TreeNode object.
 I must store additional information (string) for each node in a TreeView.
 Thank you,
  Tzigi
Avatar of Cesario Lababidi
Cesario Lababidi
Flag of Germany image

Hi tzigi,

you dont need to add any propteries to the node.
just use the Data Properties to store the String or other Information.

Simple example:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TreeView1Deletion(Sender: TObject; Node: TTreeNode);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;


var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
VAR
  Node : tTreeNode;
  s    : pString;
begin

  Node := TreeView1.items.AddChild ( nil,'FirstNode');
  new(s);
  S^ := 'Hello '+ IntToStr(Node.AbsoluteIndex);
  Node.Data := S;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  IF TreeView1.Selected <> NIL Then
    IF TreeView1.Selected.Data <> NIL
      then ShowMessage ( pString(TreeView1.Selected.Data )^);
end;

procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
  Dispose(pString(node.data));
end;

end.


Best Regards

Cesario
Avatar of tzigi
tzigi

ASKER

Thank you for your quick reply.
I know how to use the Data property, but my question was related to the concept of adding a property to an object that is dependent in other objects like in this situation the TreeView.
Can I change the TreeNode without changing the TreeView ?
  Best Regards,
  Tzigi.
ASKER CERTIFIED SOLUTION
Avatar of LukA_YJK
LukA_YJK
Flag of Azerbaijan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tzigi

ASKER

You are correct LukA.
I gave it a try but .....
The TreeNode is used in too many Functions and Procedures.
I could change the source, but that would probably violate the copyright.
I'll try another approach.
 Thank you.
Thanks! You can always use the Data property as Cesario said...