TreeView component basics? lazarus / 31-LCL ---------------------------------------------------------------------- ArtLogi · 2016-02-14 20:19 Hello .. again.. I tried to figure out the treeview component, but failing to find clear tutorials for its basic use beyond the add/remove nodes. It seems to be a powerfull component for many use, I'm myself trying to make a simple power distribution network calculator as a learning project and now I'm at point I'm starting to think I would convert my dynamic array maze to more constructed and defined "wysiwyg" component. As I noticed that the next step would be actually creating treeview functionality around of basic listview component with heritage paths and many points of data in each node (..and I don't want to follow my old maths teachers idea of "Why make it simple, if you can create it to something so beautifully complex") I have these basic questions as now:  - How I add let say two data variables for the node (as I'm assuming I must declare the variables somewhere before use, as normal in (O)Pascal, or is build in at the TreeView.)  - If I delete the node that have those 2 data variables tied to it, do I need to add routine to clear the variables before or after deleting the node it self? -  How I can then access to that data? With TreeView1.Selected.Data1Name ? As you see I'm totally a beginner. ---------------------------------------------------------------------- Blaazen · 2016-02-14 20:53 Did you try the demo in ..lazarus/examples/treeview/..  ? ---------------------------------------------------------------------- ArtLogi · 2016-02-14 21:04 Quote from: Blaazen on February 14, 2016, 09:53:56 pmDid you try the demo in ..lazarus/examples/treeview/..  ? Yep, but it only does have example of adding and removing a node, not anything how attach data for a such. Ofcourse I might be thinking it is something that it is not. There is this .data handle though, so I assume there is somesort of data layer for each node build in, but I'm a such noob of OOprogramming and swimming deeper waters than I should at this point, me thinks. ---------------------------------------------------------------------- Blaazen · 2016-02-14 21:25 TTreeView consists of Nodes (TTreeNode) and each node has property Data: Pointer where you can assign object or anything else. Also, nodes have property SelectedIndex where you can set some unique number to access the nodes. ---------------------------------------------------------------------- ArtLogi · 2016-02-14 22:44 Quote from: Blaazen on February 14, 2016, 10:25:46 pmTTreeView consists of Nodes (TTreeNode) and each node has property Data: Pointer where you can assign object or anything else. Also, nodes have property SelectedIndex where you can set some unique number to access the nodes. I think I need to get (read) knowledge about pointers before I can continue. In abstract level, when I assign some data, for sake of clarity I call it as DataValue1, to TTreeNode property called Data through pointer (which were ^NameOfPointer  IIRC) I then can access to that DataValue1 through TTreeNode in somehow and save data for it also through TTreeNode. While the underlying system (FCL) keeps the track of where the actual data is located? I'm assuming the easiest way to start fiddling with it is create a global pointer (as it doesn't matter with my small program) and then attach it to the TTreeNode. Ok, It seems this might actually go on the lines of VirtualTreeView library which have broader tutorial in wiki than TTreeView. I need to look at it closer tomorrow with fresh eyes. ---------------------------------------------------------------------- wp · 2016-02-15 00:09 I am attaching a short demo project which shows how you can add data to tree nodes. The data in the project are defined by the class "TMyData". When you click on "Add" or "Add child" a form comes up into which you can enter some data. After closing the form an instance of this class is created, a new node is added to the tree and the TMyData instance is appended to the node via its Data pointer. It is important to destroy the TMyData instances whenever a tree node is deleted, or whenever the tree is cleared or destroyed. For this purpose add code to the OnDeletion event of the tree. There is no need for any global variables! [EDIT] As shown in the other discussion (http://forum.lazarus.freepascal.org/ index.php/topic,31525.msg202077.html#msg202077) the sample project "TreeViewWithData.zip" contains a memory leak. Please use "TreeViewWithData-correct.zip" instead. ---------------------------------------------------------------------- ArtLogi · 2016-02-15 12:14 Quote from: wp on February 15, 2016, 01:09:54 amI am attaching a short demo project which shows how you can add data to tree nodes. The data in the project are defined by the class "TMyData". When you click on "Add" or "Add child" a form comes up into which you can enter some data. After closing the form an instance of this class is created, a new node is added to the tree and the TMyData instance is appended to the node via its Data pointer. It is important to destroy the TMyData instances whenever a tree node is deleted, or whenever the tree is cleared or destroyed. For this purpose add code to the OnDeletion event of the tree. There is no need for any global variables! Thank you wery much, that indeed clarifies the usage of the component significantly and shows more of its potential. Good demo as it actually is doing something potentially usefull, where the TV_demo in the example folder is more of a teaser, for someone like me who is not too familiar of the language nor OOP (which is just falsely named, it should be "Subprogram interconnection method"  ). ---------------------------------------------------------------------- JuhaManninen · 2016-02-15 13:14 The demo in the example folder can also be improved. Patches are welcome. ---------------------------------------------------------------------- ArtLogi · 2016-02-15 16:04 OK, the parent routine weren't as simple as I though.. Anyways here is two "extensions" for the WPs example: Free to use.. (*Lists ALL nodes before selected node in the TreeView, also from different branches.*) 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.PreviusNodesClick(Sender: TObject);  var    node: TTreeNode;begin (*Lists ALL nodes before selected node in the TreeView.*)    node := TreeView1.Selected;        (*Get selection*)   if node = nil then exit;           (*If no selection, do nothing*)  repeat      begin        Memo1.Lines.Add(node.Text);    (*Add results to memofield*)        node := node.GetPrev;    (*Get previus node in the "list"*)      end;    until node = nil;end;         Shows parent node and 1st node in the branch 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.ShowParentsClick(Sender: TObject);  var    node: TTreeNode;    ownernode: TTreeNode;begin   node := TreeView1.Selected;                                     (*get selected node*)  if node = nil then exit;                                           (*If no selection do nothing*)    // Note! Next line will cause a crash if selection is root of the brach. Reason: to keep this example clear and simple.   ownernode := node.GetParentNodeOfAbsoluteLevel(node.level - 1);   (*Get parent of the selected node*)   Memo1.Lines.Add('Parent ' + ownernode.text);                      (*show result in memofield*)   ownernode:= node.GetParentNodeOfAbsoluteLevel(0);                 (*Get the 1st node in the branch*)   Memo1.Lines.Add('1st in branch ' + ownernode.text);               (*show result in memofield*)end; Both are functionality of a button in form1. ---------------------------------------------------------------------- taazz · 2016-02-15 16:25 Quote from: ArtLogi on February 15, 2016, 05:04:32 pmShows parent node and 1st node in the branch 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.ShowParentsClick(Sender: TObject);  var    node: TTreeNode;    ownernode: TTreeNode;begin   node := TreeView1.Selected;                                     (*get selected node*)  if node = nil then exit;                                           (*If no selection do nothing*)    // Note! Next line will cause a crash if selection is root of the brach. Reason: to keep this example clear and simple.   ownernode := node.GetParentNodeOfAbsoluteLevel(node.level - 1);   (*Get parent of the selected node*)   Memo1.Lines.Add('Parent ' + ownernode.text);                      (*show result in memofield*)   ownernode:= node.GetParentNodeOfAbsoluteLevel(0);                 (*Get the 1st node in the branch*)   Memo1.Lines.Add('1st in branch ' + ownernode.text);               (*show result in memofield*)end; Both are functionality of a button in form1. Node.Parent returns a nodes parent/owner no need of that ofabsolutelevel calls and the top most node can be found using treeview1.items[0] (if memory serves me right). ---------------------------------------------------------------------- ArtLogi · 2016-02-15 16:30 Oh, how did I miss that .parent  Thx, that will make the routine for tracing back to root much simpler. Quote from: taazz on February 15, 2016, 05:25:52 pm Node.Parent returns a nodes parent/owner no need of that ofabsolutelevel calls and the top most node can be found using treeview1.items[0] (if memory serves me right). The treeview1.items[0] might not work, if you have more than one branch starting from the root? (Need to run to market, so no time to test atm.) ---------------------------------------------------------------------- ArtLogi · 2016-04-01 13:47 Hello again. For a long time I have been again coding this, trying to construct something that will crawl through whole family / Treebranch from down to up (which seems to be two notches harder task than up to down. I need to figure out how to make while-do -loop stack with dynamic depth maybe not some getnext + parent combination actually do the same. anyways this is a sidetrack ). Then I noticed that .GetLastSubChild behaves oddly in my mind.. is this a bug or is it behaving as should? abcdThis gives as a lastsubchild "1. D" abcdeeddGives by my observation "3. D" not "2.e" Is this correct behaviour?? I'm running Lazarus 1.6RC2.. ---------------------------------------------------------------------- Cyrax · 2016-04-01 18:31 Please update to proper 1.6 release of Lazarus. Release candidates are only for testing. ---------------------------------------------------------------------- ArtLogi · 2016-04-01 21:08 Quote from: Cyrax on April 01, 2016, 08:31:12 pmPlease update to proper 1.6 release of Lazarus. Release candidates are only for testing. I see now where the RC comes from, live and learn. ---------------------------------------------------------------------- ArtLogi · 2016-06-19 21:09 I just tried to make a jelly from my brains, but didn't succeed. Instead I finally got around of making my top-down crawler, first on paper and then on code. Lots of interesting things learned today, ie. how to use procedures and that you can call them inside of them self.  *insert dancing smilyface here* Edit. Removed the code. There were a logic mistake that prevent it to be general solution and I assume it had one layer of unnecessary complexity (as doing backward calls) I need to prototype more of these methods .. *hmpf* The handless/methods or how they are called (.GetLastSubChild, GetLastExpandedSubNode etc..) are really messy at what they do. Like GetLastSubChild I can't get hold of what is logic behind its behaviour. I expected it to be like  .GetLastExpandedSubNode (assuming that it really is "Absolute last node" as source comment indicates) and .GetLastSibling applied to that absolute tip of the branch. Nope it somewhat seems to work as would .GetLastChild with twist. The sequence for a following tree with .GetLastSubChild: 1   2     3       4         5     3.2   2.2 ..is as follows, starting from node 1 -> 2.2, From node 2 -> 3.2, From node 3 -> 5 Soo.. Back to the grinder.