Adding a new property (String) to a TreeNode.

tzigi
tzigi used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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

Author

Commented:
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.
Commented:
No... I think... Just try it, good luck. Anyhow I think the changes should not be very big...

Author

Commented:
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.

Commented:
Thanks! You can always use the Data property as Cesario said...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial