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

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:
You should be able to edit the TreeNode with making a class of (TTreeNode) and adding your own property to it.

You would only have to use an edited TreeView for it (AFAIK), because you can only add TreeNodes to a TreeView with the internal TreeView Methods.

For example the Add Method calls the AddObject Method that is:

function TTreeNodes.AddObject(Node: TTreeNode; const S: string;
  Ptr: Pointer): TTreeNode;
begin
  if Node <> nil then Node := Node.Parent;
  Result := InternalAddObject(Node, S, Ptr, taAdd);
end;

InternalAddObject is:

function TTreeNodes.InternalAddObject(Node: TTreeNode; const S: string;
  Ptr: Pointer; AddMode: TAddMode): TTreeNode;
var
  Item: HTreeItem;
begin
  Result := Owner.CreateNode;
  try
    if Node <> nil then Item := Node.ItemId
    else Item := nil;
    Result.Data := Ptr;
    Result.Text := S;
    Item := AddItem(Item, nil, CreateItem(Result), AddMode);
    if Item = nil then
      raise EOutOfResources.Create(sInsertError);
    Result.FItemId := Item;
    if (FUpdateCount = 0) and (Result = GetFirstNode) then
      SendMessage(Handle, WM_SETREDRAW, 1, 0);
    AddedNode(Node);
  except
    Result.Free;
    raise;
  end;
end;

function TCustomTreeView.CreateNode: TTreeNode;
begin
  Result := TTreeNode.Create(Items);
end;

Here you would have to use
Result:= TMyTreeNode.Create(Items);


You can just make your own TreeView from a TTreeView and overwrite the CreateNode Method.

I think this should work.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
Thank you Marc,
  I'll try it.

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

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

Author

Commented:
Thank you All.
 

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