[Solved] Is it possible to assign a unique id to TTreeNode? lazarus / 31-LCL ---------------------------------------------------------------------- loaded · 2022-02-17 13:27 Hi All, Is there a feature in TreeView that I can link to the relevant unique value when I want to delete a selected TTreeNode? So that after the TTreeNode is deleted, that value is not used again. ---------------------------------------------------------------------- balazsszekely · 2022-02-17 13:43 Quote from: loaded on February 17, 2022, 02:27:22 pmHi All, Is there a feature in TreeView that I can link to the relevant unique value when I want to delete a selected TTreeNode? So that after the TTreeNode is deleted, that value is not used again. Node.AbsoluteIndex is unique at a certain point, but if you delete a node the tree is re-indexed, not good for you. Please declare a variable in your private section of your form,  FUniqueID for example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin  FUniqueID := 0;end; When you add a node, assign FUniqueID to Node.Data: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Node: TTreeNode;begin  Inc(FUniqueID);  Node := TreeView1.Items.Add(nil, 'test test');  Node.Data := @FUniqueID; end;   Retrieve  uniqueid when needed: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  UniqueID: Integer;  Node: TTreeNode;begin   Node := TreeView1.Selected;   if Node <> nil then   begin     UniqueID := Integer(Node.Data^);     ShowMessage(UniqueID.ToString);   end;end; ---------------------------------------------------------------------- loaded · 2022-02-17 13:56 My dear Master GetMem, This information made my job a lot easier. I suddenly changed my mind, now I can also connect TJSONData here. Once again I am grateful to you. Respects. ---------------------------------------------------------------------- wp · 2022-02-17 14:05 Quote from: GetMem on February 17, 2022, 02:43:38 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Node.Data := @FUniqueID; Really? Don't all nodes have the same Data then? Because all Node.Data point to the same variable. I think what you mean is this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  FUniqueID: PtrInt;  // Must have the same size as a pointer...  Node.Data := Pointer(FUniqueID); ---------------------------------------------------------------------- Thaddy · 2022-02-17 14:15 Pointers, by their very nature, can not be negative, so FUniqueID := PtrUint; IOW an unsigned type of size pointer. (Although in most cases the answer by wp will work) ---------------------------------------------------------------------- balazsszekely · 2022-02-17 14:30 @wp QuoteReally? Don't all nodes have the same Data then? Because all Node.Data point to the same variable. I think what you mean is this:True, silly mistake. Thanks for the correction! @Thaddy QuotePointers, by their very nature, can not be negative, so FUniqueID := PtrUint; IOW an unsigned type of size pointer. (Although in most cases the answer by wp will work) The main problem with my code is that each node will have the same value. I type my response without actually testing it, usually get away with it, not this time. ---------------------------------------------------------------------- loaded · 2022-02-17 16:33 Thank you very much for your warning and information, respected masters wp and Thaddy and GetMem I would like to ask one more question about the pointer; Can we use the property of the relevant object over the pointer? I remember as if I saw something like this somewhere. Or am I wrong? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Form1: TForm1;  nodes:TTreeNode;  jstext:String='{"features": [{"type": "Feature","geometry": {"type": "MultiPolygon","coordinates": [[[[32.49 998,37.98428],[32.50025,37.98395],[32.50058,37.98334],[32.49871,37.981 41],[32.49822,37.98179],[32.49825,37.98182],[32.49811,37.98244],[32.49 754,37.9844],[32.49754,37.98479],[32.49726,37.98544],[32.49794,37.9852 ],[32.49965,37.98447],[32.49998,37.98428]],[[32.49922,37.98431],[32.49 919,37.98426],[32.49926,37.98423],[32.49929,37.98429],[32.49922,37.984 31]]]]}}],"type": "FeatureCollection","crs": {"type": "name","properties": {"name": "EPSG:4326"}}}';  jsn:TJSONData;implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  nodes:=TreeView1.Items.Add(nil,'dede');  jsn:=GetJSON(jstext);  nodes.Data:=@jsn;end; procedure TForm1.TreeView1Click(Sender: TObject);begin  if TreeView1.Selected<>nil then  begin  //showmessage(jsn.FindPath('type').AsString);    showMessage(TreeView1.Selected.@Data^.FindPath('type').AsString);  //Is it possible to access methods via pointer in this wrong line?  end;end; ---------------------------------------------------------------------- Remy Lebeau · 2022-02-17 17:28 Quote from: loaded on February 17, 2022, 05:33:43 pmCan we use the property of the relevant object over the pointer? I remember as if I saw something like this somewhere. Or am I wrong? You need to type-cast the TTreeNode.Data pointer in order to access any functionality of the object it is pointing at, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Form1: TForm1;  jstext:String='{"features": [{"type": "Feature","geometry": {"type": "MultiPolygon","coordinates": [[[[32.49998,37.98428],[32.5002 5,37.98395],[32.50058,37.98334],[32.49871,37.98141],[32.49822,37.98179 ],[32.49825,37.98182],[32.49811,37.98244],[32.49754,37.9844],[32.49754 ,37.98479],[32.49726,37.98544],[32.49794,37.9852],[32.49965,37.98447], [32.49998,37.98428]],[[32.49922,37.98431],[32.49919,37.98426],[32.4992 6,37.98423],[32.49929,37.98429],[32.49922,37.98431]]]]}}],"type": "FeatureCollection","crs": {"type": "name","properties": {"name": "EPSG:4326"}}}'; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  node: TTreeNode;  jsn: TJSONData;begin  node := TreeView1.Items.Add(nil, 'dede');  jsn := GetJSON(jstext);  node.Data := jsn; // <-- no @ here!end; procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);begin  TJSONData(Node.Data).Free;end; procedure TForm1.TreeView1Click(Sender: TObject);var  node: TTreeNode;begin  node := TreeView1.Selected;  if node <> nil then  begin    ShowMessage(TJSONData(node.Data).FindPath('type').AsString);  end;end; ---------------------------------------------------------------------- loaded · 2022-02-17 17:50 Quote from: Remy Lebeau on February 17, 2022, 06:28:02 pmYou need to type-cast the TTreeNode.Data pointer in order to access any functionality of the object it is pointing at, eg: Thank you very much Remy Lebeau. Now I got what I wanted. This forum is full of real heroes. God bless all the masters in this forum and give them long life.