[SOLVED] Assigning nested procedure inside a helper lazarus / 21-General ---------------------------------------------------------------------- indydev · 2021-11-01 23:38 I have been taking advantage of Helper methods to components and decided to add an inplace option to a Listbox. But I am having trouble assigning a nested procedure to onXXX events of my own created object (TEdit) at runtime. I have added {$modeswitch nestedprocvars}  to the directives. I can't seem to figure out how/where to make a variable procedural type so that the nested procedure will be referenced, and then have it assigned to an onExit (onKeyDown, etc.) event. I know that what I have isn't right, but I hope the code presents clearly what I am trying to do. 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 Type    TNestedProc = procedure is nested  // <--something like this is needed. .... procedure TListboxHelper.InPlaceEdit(Sender: TOBject; Button: TMouseButton;                        Shift: TShiftState; X, Y: Integer);var  r: TRect;    Ed: Tedit;    procedure EdExit(Sender: TObject); // <-- Nested procedure    begin          Self.Items[Self.ItemIndex] := (Sender as TEdit).Text;    end; begin   if Self.ItemIndex < 0 then exit;         Ed:= TEdit.Create(Self);       Ed.Font.Size:=10;         Ed.Parent := Self;         r:= Self.ItemRect(Self.ItemIndex);         r.topleft := ClientToScreen( r.topleft );         r.BottomRight := ClientToScreen( r.bottomright );         r.topleft := ScreenToClient( r.topleft );         r.BottomRight := screenToClient( r.bottomright );         Ed.text := Self.Items[Self.ItemIndex];         Ed.setbounds( r.left, r.top-2,              Self.ClientWidth,                r.bottom-r.top+4 );       Ed.OnExit := @EdExit(Sender);  // <-- Nested call         //Ed.OnMouseDown := ??         //Ed.OnKeyPress := ??         SetCapturecontrol(Ed);         Ed.SetFocus;end; ---------------------------------------------------------------------- PascalDragon · 2021-11-02 11:45 Quote from: indydev on November 02, 2021, 12:38:27 amI have been taking advantage of Helper methods to components and decided to add an inplace option to a Listbox. But I am having trouble assigning a nested procedure to onXXX events of my own created object (TEdit) at runtime. I have added {$modeswitch nestedprocvars}  to the directives. I can't seem to figure out how/where to make a variable procedural type so that the nested procedure will be referenced, and then have it assigned to an onExit (onKeyDown, etc.) event. I know that what I have isn't right, but I hope the code presents clearly what I am trying to do. You can't assign a nested routine to a method variable. Declaring your EdExit as a private method in the helper and assigning that to OnExit should work. ---------------------------------------------------------------------- Imants · 2021-11-02 13:03 Try something like 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 Type    TNestedProc = procedure is nested  // <--something like this is needed.  TNestedComponent = class(TComponent)  private    FOnExit: TNestedProc;  public    procedure EdExit(Sender: TObject); // <-- Nested procedure                property OnExit: TNestedProc read FOnExit write FOnExitend; procedure TNestedComponent.EdExit(Sender: TObject); // <-- Nested procedure begin  FOnExit(Sender)end; procedure TListboxHelper.InPlaceEdit(Sender: TOBject; Button: TMouseButton;                      Shift: TShiftState; X, Y: Integer);var  r: TRect;  Ed: Tedit;        cmp: TNestedComponent;    procedure EdExit(Sender: TObject); // <-- Nested procedure    begin          Self.Items[Self.ItemIndex] := (Sender as TEdit).Text;    end; begin   if Self.ItemIndex < 0 then exit;         Ed:= TEdit.Create(Self);           cmp := TNestedComponent.Create(Ed);                 cmp.EdExit = @EdExit;         Ed.Font.Size:=10;         Ed.Parent := Self;       r:= Self.ItemRect(Self.ItemIndex);         r.topleft := ClientToScreen( r.topleft );         r.BottomRight := ClientToScreen( r.bottomright );         r.topleft := ScreenToClient( r.topleft );       r.BottomRight := screenToClient( r.bottomright );         Ed.text := Self.Items[Self.ItemIndex];         Ed.setbounds( r.left, r.top-2,                Self.ClientWidth,                r.bottom-r.top+4 );       Ed.OnExit := @cmp.EdExit;  // <-- Nested call         //Ed.OnMouseDown := ??         //Ed.OnKeyPress := ??         SetCapturecontrol(Ed);         Ed.SetFocus;end; ---------------------------------------------------------------------- indydev · 2021-11-02 16:43 I really want to thank everyone for answering. Each answer gets more specific. There is a lot of creating and then passing addresses around going on, so I am trying take it all in. Using Imants example (who kindly embedded into my posted code), I have written some comments illustrating my thoughts on how this works. No requirement to respond, but if you see that I have it wrong I would appreciate the correction (even minor terminology errors often end up being important). I added a destruction method call to my created TEdit, and am trying to figure out where to destroy my TNestedComponent still.  Also, is the Property OnExit really 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 {$modeswitch nestedprocvars}  Type    TNestedProc = procedure is nested  // <--Nested Procedure Type.  TNestedComponent = class(TComponent)  private    FOnExit: TNestedProc;  // <--Nested procedure assigned to component variable.  public    procedure EdExit(Sender: TObject); // <-- procedure declaration inside a class set for nested assignment.           property OnExit: TNestedProc read FOnExit write FOnExit  // <-- ?? is this used or needed?end; procedure TNestedComponent.EdExit(Sender: TObject); // <-- written for execution of Nested procedural variable.begin  FOnExit(Sender)end; procedure TListboxHelper.InPlaceEdit(Sender: TOBject; Button: TMouseButton;                      Shift: TShiftState; X, Y: Integer);var  r: TRect;  Ed: Tedit;   cmp: TNestedComponent;  // <-- declaration of Nested Component (which is designed to handle Nested Procedure)        procedure EdExit(Sender: TObject); // <-- Nested procedure    begin        Self.Items[Self.ItemIndex] := (Sender as TEdit).Text;           (Sender as TEdit).Destroy;    // <--need to destroy or free Ed here?  end; begin     if Self.ItemIndex < 0 then exit;         Ed:= TEdit.Create(Self);         cmp := TNestedComponent.Create(Ed); // <-- create the new component and assign it to Ed.         cmp.EdExit = @EdExit; // <-- Assign nested procedure to the new component.         Ed.Font.Size:=10;         Ed.Parent := Self;         r:= Self.ItemRect(Self.ItemIndex);         r.topleft := ClientToScreen( r.topleft );         r.BottomRight := ClientToScreen( r.bottomright );         r.topleft := ScreenToClient( r.topleft );         r.BottomRight := screenToClient( r.bottomright );         Ed.text := Self.Items[Self.ItemIndex];         Ed.setbounds( r.left, r.top-2,              Self.ClientWidth,                r.bottom-r.top+4 );       Ed.OnExit := @cmp.EdExit;  // <-- Nested call         //Ed.OnMouseDown := ??         //Ed.OnKeyPress := ??         SetCapturecontrol(Ed);         Ed.SetFocus;end; ---------------------------------------------------------------------- Imants · 2021-11-02 17:53 "and am trying to figure out where to destroy my TNestedComponent still." It will be destroyed together with TEdit. Components are destroyed together with parent and you do not need to do anything for it to happen. 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    Cmp1, Cmp2, Cmp3: TComponent;begin    Cmp1 := TComponent.Create(nil);    Cmp2 := TComponent.Create(Cmp1);    Cmp3 := TComponent.Create(Cmp1);    Cmp1.Free; //Will destroy Cmp2 and Cmp3 automaticallyend. ---------------------------------------------------------------------- indydev · 2021-11-02 18:50 "It will be destroyed together with TEdit. Components are destroyed together with parent and you do not need to do anything for it to happen." Ah right, of course. Thank you. What role is the property onExit playing? ---------------------------------------------------------------------- Imants · 2021-11-03 06:54 "What role is the property onExit playing?" In your case nothing I just usually use them ---------------------------------------------------------------------- indydev · 2021-11-03 17:53 Ok, after finally getting in to implement this the compiler is complaining at line 36 (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 cmp.EdExit := @EdExit; Because FOnExit inside the cmp.EdExit method requires a (sender) parameter. But after adding that, then I needed to add it to the nested @EdExit side 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 cmp.EdExit(Sender) := @EdExit(Sender);But then it claims "Variable Identifier Expected." I have the compiler switch nestedprocvars on. ---------------------------------------------------------------------- Imants · 2021-11-03 19:04 If this is class definition: 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 TNestedComponent = class(TComponent)  private    FOnExit: TNestedProc;  // <--Nested procedure assigned to component variable.  public    procedure EdExit(Sender: TObject); // <-- procedure declaration inside a class set for nested assignment.           property OnExit: TNestedProc read FOnExit write FOnExit  // <-- ?? is this used or needed?end; Then right way t o assign would be: 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 cmp.OnExit := @EdExit; You can assign only to variable EdExit is procedure cmp.EdExit(Sender) := @EdExit(Sender); This will never work because if you add brackets  () that mean that you are trying to call method not assign its address ---------------------------------------------------------------------- indydev · 2021-11-03 19:49 Ok, so the property is useful here. Assignment using OnExit worked! Thank you.