OnCollapsed event in TreeView fires when treeview is cleared and rebuilt lazarus / 31-LCL ---------------------------------------------------------------------- DmytroLev · 2020-11-14 13:59 Hello! Could anybody please advise me on such issue: I have a TreeView with some data I fetch from a database. I check the OnCollapsed / OnExpanded events to save the status of the nodes (expanded / collapsed) for a user's convenience next time the TreeView is opened (i.e. next time the application is run, or after another SQL query gets renewed information from the DB). This works ok. But the OnCollapsed event also fires when the app is closed or the TreeView is cleared, resulting in ALL nodes being regarded as "collapsed" (closed), which is not what I want. How can I make the OnCollapsed event to fire only in case when a user collapses the node? ---------------------------------------------------------------------- Sieben · 2020-11-14 14:19 I'm afraid you will have to use some kind of flag to exit the event handler if you don't want to have it executed. Or perhaps submit a bug report. This issue has also been discussed here: https://forum.lazarus.freepascal.org/index.php/topic,35958.msg383367.h tml ---------------------------------------------------------------------- Blaazen · 2020-11-14 14:42 I just tested and this seems to work: 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 if not (csDestroying in TreeView1.ComponentState) then ... inside the OnCollapsed event. ---------------------------------------------------------------------- DmytroLev · 2020-11-14 16:01 Thank you! It works for closing the app, but still doesn't work in case of clearing the TreeView before renewing the data. But I'll try to find out something in that direction. Thanks again. ---------------------------------------------------------------------- Sieben · 2020-11-14 16:10 You can use a form flag like eg FSuspended and set it true before clearing and back to false after. Not too nice but works. 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 if not FSuspended and not (csDestroying in TreeView1.ComponentState) then ... ---------------------------------------------------------------------- DmytroLev · 2020-11-14 16:18 Thank you, that worked!  Thanks again Sieben and Blaazen for your quick and useful responds! My issue is solved. ---------------------------------------------------------------------- jamie · 2020-11-15 00:03 I believe this should be reported because I just checked my old Delphi and both of the events do not get called during destruction of the control and clearing it.  There is no logic for these events to be called other than someone's needed hacked to satisfy their needs. ---------------------------------------------------------------------- jamie · 2020-11-15 01:59 Ok, I figured it out...  when ever the collapsed or collapsing events are called, you test the "Deleting" property in the given node that is pass to the event.. 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.TreeView1Collapsed(Sender: TObject; Node: TTreeNode);begin if node.Deleting then beep;end;                         so this all works out well. it gives you the chance to do something with it before its gone.. when clearing or freeing the control this Boolean is set so all you need to do is test this during normal execution 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  If not node.deleting then    begin     //your code;   end; ---------------------------------------------------------------------- jamie · 2020-11-15 14:09 I reported this because this does not happen in Delphi. Delphi handles this correctly, it does not call the events during destruction or clearing from what I can see.  Tested with Delphi 10.3.3 ---------------------------------------------------------------------- JuhaManninen · 2020-11-15 15:22 Quote from: DmytroLev on November 14, 2020, 02:59:01 pmBut the OnCollapsed event also fires when the app is closed or the TreeView is cleared ... How do you clear the TreeView? ---------------------------------------------------------------------- Blaazen · 2020-11-15 15:33 I tested 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 TreeView1.Items.Clear; and I got on OnCollapsed events. ---------------------------------------------------------------------- JuhaManninen · 2020-11-15 16:58 Please test with r64142. ---------------------------------------------------------------------- Sieben · 2020-11-15 17:12 Seems like the event isn't fired anymore with Items.Clear nor on destroy. May I ask what exactly has been changed? ---------------------------------------------------------------------- bytebites · 2020-11-15 17:18 svn diff -r 64141:64142 ---------------------------------------------------------------------- Blaazen · 2020-11-15 17:26 You can watch it online: https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/lcl/include/treevi ew.inc?root=lazarus&r1=64142&r2=64141&pathrev=64142 ---------------------------------------------------------------------- jamie · 2020-11-15 17:30 I just tested the trunk and it seems to be working correctly now.. during destroy or items.clear, the events do not get called here.. ---------------------------------------------------------------------- Sieben · 2020-11-15 17:42 @bytebites - Thanks. ---------------------------------------------------------------------- Blaazen · 2020-11-15 18:00 QuoteI just tested the trunk and it seems to be working correctly now.. during destroy or items.clear, the events do not get called here.. Yes, no OnCollapsed in 64142. ---------------------------------------------------------------------- DmytroLev · 2020-11-16 20:06 Thanks again everybody!