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 Stefaan
Stefaan

You can add whatever information to a TTreeNode using the data property :

The following code defines a record type of TMyRec and a record pointer type of PMyRec.

type
PMyRec = ^TMyRec;
TMyRec = record
  FName: string;
  LName: string;
end;

Assuming these types are used, the following code adds a node to TreeView1 as the last sibling of a specified node. A TMyRec record is associated with the added item. The FName and LName fields are obtained from edit boxes Edit1 and Edit2. The Index parameter is obtained from edit box Edit3. The item is added only if the Index is a valid value.

procedure TForm1.Button1Click(Sender: TObject);

var
  MyRecPtr: PMyRec;
  TreeViewIndex: LongInt;
begin
  New(MyRecPtr);
  MyRecPtr^.FName := Edit1.Text;
  MyRecPtr^.LName := Edit2.Text;
  TreeViewIndex := StrToInt(Edit3.Text);
  with TreeView1 do
  begin
    if Items.Count = 0 then
      Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
    else if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then

      Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);
  end;
end;
Avatar of 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 MarcG
MarcG

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

Thank you Marc,
  I'll try it.
Hi again,

Well you could do it of course, but it seems to be pretty hard.  You would have to subclass the TTreeNode and the TTreeNodes, and apparently some stuff is declared as Private variables, so it seems to be pretty hard to get it working correctly.  You would even have to subclass some treeview aspects.  So you will end up with your own derived TTreeView, TTreeNodes and TTreeNode objects.

Best regards,


Stefaan
Damn, too late with posting my comment, Marc pressed the post button before I did, but what he actually says is exactly the same thing.
Avatar of tzigi

ASKER

Thank you All.