TTreeView - OnCreateNodeClass & OnCustomCreateItem lazarus / 31-LCL ---------------------------------------------------------------------- egsuh · 2020-03-30 10:51 I tried OnCreateNodeClass alone, but this alone does not work correctly. Instead I also have to define OnCustomCreateItem event handler. 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 TOpenCoding.TV1CreateNodeClass(Sender: TCustomTreeView;   var NodeClass: TTreeNodeClass);begin    NodeClass := TOpenNode;end; procedure TOpenCoding.TV1CustomCreateItem(Sender: TCustomTreeView;   var ATreeNode: TTreenode);begin   ATreeNode := TOpenNode.Create(Sender.Items);end;  There are some explanations that I only have to define OnCreateNodeClass. Should I define both event handlers or am I doing something wrong? ---------------------------------------------------------------------- lucamar · 2020-03-30 12:17 Both, otherwise the class you tell is created in OnCreateNodeClass and the real node item class will be different. IIRC (I might not, it's been quite some time) it's a bug in the tree view code: it should create items of the class you tell it but it doesn't. ---------------------------------------------------------------------- martijnn · 2020-03-31 07:26 Quote from: egsuh on March 30, 2020, 12:51:18 pmI tried OnCreateNodeClass alone, but this alone does not work correctly. Instead I also have to define OnCustomCreateItem event handler. Could you indicate a bit more what does not work correctly? In the TCustomTreeView.CreateNode method OnCustomCreateItem is tried first when a handler is attached. In case no node instance is returned from there OnCreateNodeClass is attempted (when a handler is present). So using one of the event handlers should be sufficient. Are you using a TTreeView or a variant (e.g. TShellTreeView) ? Which Lazarus version? ---------------------------------------------------------------------- egsuh · 2020-03-31 07:44 QuoteCould you indicate a bit more what does not work correctly? With assigning only "OnCreateNodeClass", it simply fails --- class type mismatch (run-time error). Sometimes it works fine if there are only one variable or property in the new node type (TOpenNode in my example), but not when there are more than two fields / properties. ---------------------------------------------------------------------- martijnn · 2020-03-31 08:31 Quote from: egsuh on March 31, 2020, 09:44:55 amQuoteCould you indicate a bit more what does not work correctly? With assigning only "OnCreateNodeClass", it simply fails --- class type mismatch (run-time error). Does that happen when accessing the nodes properties? Quote from: egsuh on March 31, 2020, 09:44:55 amQuoteCould you indicate a bit more what does not work correctly? Sometimes it works fine if there are only one variable or property in the new node type (TOpenNode in my example), but not when there are more than two fields / properties. Can't reproduce that. Do you have a minimal code example that produces the exception? ---------------------------------------------------------------------- egsuh · 2020-03-31 09:11 Following is the core of the process. Dataset is ordered by field NewCode.  If the NewCode is different from previous one, it is added as a root-level tree node, and if it is the same as the previous one, it is added as child node. But this is not an issue --- you may use loop, e.g. for i=1 to 20, etc. Declaring "pid" as property, not a string type variable is irrelevant. I just wanted to test both field and property. 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   { TOpenNode }   TOpenNode = class(TTreeNode)  private     FPid: string;     rid,     qno,     rorder,     rcode,     NewCode,     ParentCode : integer;     RText: string;  public     property pid : string read FPid write FPid;      procedure SaveToDB;     procedure CopyOtherNode(snode: TOpenNode);  end;  procedure TOpenCoding.LoadCodeFrametoTV(tpid:string; DSet: TDataSet);var   nc, nc0: integer;   ntp1, ntp2 : TTreeNode;begin   with (DSet as TBufDataSet) do begin      nc0 := 0;      First;      while not eof do begin        nc := Fieldbyname('NewCode').AsInteger;        if (nc = 0) or (nc <> nc0)            then ntp1 := nil  // ntp1 is parent node.           else ntp1 := ActTV.Items.GetLastNode; //   ActTV is the name of TreeView         ntp2 := ActTV.Items.AddChild (ntp1, FieldbyName('RText').AsString);         with (ntp2 as TOpenNode) do begin  // <== Error occurs here.. within the with block.            pid := tpid;                                             Rid := FieldbyName('RiD').AsInteger;           Qno := FieldbyName('Qno').AsInteger;           ROrder := FieldbyName('ROrder').AsInteger;           RCode := FieldbyName('Rcode').AsInteger;           RText := FieldbyName('RText').AsString;           NewCode := FieldbyName('NewCode').AsInteger;           ParentCode := FieldbyName('ParentCode').AsInteger;        end;         nc0 := nc;      Next;      end; // while not cdsOpen.eof   end; // with cdsOpenend; ---------------------------------------------------------------------- martijnn · 2020-03-31 11:20 Still not reproducible. Only if OnCreateNodeClass of treeview is not assigned you'll get a runtime error. I've attached a simple test project without using the dataset instance. Maybe you could modify it to expose the undesired behavior that you're encountering. ---------------------------------------------------------------------- egsuh · 2020-04-02 03:49 @martijnn, You are right. I cannot reproduce the exception with your example. But in my own program, the exception still occurs, EInvalidCast  at line         with (ntp2 as TOpenNode) do begin Once I assign  OnCustomCreateItem then there are no exceptions For the time being, I'll continue looking for possible other reasons, and post here once I find it. ---------------------------------------------------------------------- JuhaManninen · 2020-04-02 07:29 @egsuh, you did not tell your Lazarus version. IIRC this was fixed at some point. [Edit] Ok, I didn't find any recent changed for this feature. The latest may be r48183 by Bart, already 5 years ago. ---------------------------------------------------------------------- egsuh · 2020-04-03 04:13 I found the reason... it was because I defined OnCreateNodeClass event handler in a frame, but in the form's .lfm file, OnCreateNodeClass was set to nil under inherited TreeView1 (I should have pressed the place in the object inspector inadvertently). What would you recommend in these cases? I have similar experiences, and in those situations, I opened .lfm file, and removed the parts of "inherited .... ".  Is this the only way?  Or would you recommend to re-assign in the forms' event handlers again, e.g. in this case, form1.oncreatenodeclass = frame1oncreatenodeclass, etc. ? It is possible but in this case I have to look for all event handlers defined in the frame, and assign all of them on the form.