[ SOLVED ] Frame and procedure lazarus / 21-General ---------------------------------------------------------------------- superc · 2022-04-20 14:48 Hello, I've a Form with 4 TTabShett , in one of these there are loaded at runtime a TTframe; now I need to call a procedure of the parent form from the frame for refresh components into frame. I created a procedure in the Frame can accept tobject, and I pass parent form and inside the frame I cast and call the procedure, but is a good methods? Thanks in advance. ---------------------------------------------------------------------- mas steindorff · 2022-04-20 16:51 I use the following code a lot, not just for Frames, but also in my classes so they can talk back to their owner only if the owner is able to handle the info.  if the owner does not "register" then the info is just ignored. in your frame code... 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   TShowStatusEvent   = procedure(str:string) of object; // to pass strings back =>GUI // then in the type section of your  tFrame... "TmyFrame" in this example private   fOnShowStatus :TShowStatusEvent;  //    ...public    property OnShowStatus :TShowStatusEvent   read FOnShowStatus     Write FOnShowStatus; ...end;   and later... 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 TmyFrame.PassStr(Str);begin  if assigned(FOnShowStatus) then    begin      FOnShowStatus(str);    end;end;  then your frame code will pass this stuff on to the procedure (if any) that has been "registered ". in the owners code, perhaps in the create ... 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  // myFrame := TmyFrame.create(myPanel4frame); // only needed if you create at runtime ... myFrame.OnShowStatus := @myRevieverOfStrings; // where myRevieverOfStrings is ...procedure Tform.myRevieverOfStrings(str:string)begin // do something with string or ignore itend;  "string" can be replaced with a variable or a even a record.  I tend to make separate call backs for status info and for debug info for more complexes classes. if ignoring is not acceptable, you can get aggressive and showmessage(info) or add a visual indicator on your frame. MAS ---------------------------------------------------------------------- moessner · 2022-04-20 17:12 I wish thank a lot. moessner - mario piva ---------------------------------------------------------------------- superc · 2022-04-21 06:37 Quote from: mas steindorff on April 20, 2022, 06:51:48 pmI use the following code a lot, not just for Frames, but also in my classes so they can talk back to their owner only if the owner is able to handle the info.  if the owner does not "register" then the info is just ignored. in your frame code... 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   TShowStatusEvent   = procedure(str:string) of object; // to pass strings back =>GUI // then in the type section of your  tFrame... private   fOnShowStatus :TShowStatusEvent;  //    ...public    property OnShowStatus :TShowStatusEvent   read FOnShowStatus     Write FOnShowStatus; ...end;   and later... 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 TThreadComm.PassStr(Str);begin  if assigned(FOnShowStatus) then    begin      FOnShowStatus(str);    end;end;  then your frame code will pass this stuff on to the procedure (if any) that has been "registered ". in the owners code, perhaps in the create ... 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  myFrame.OnShowStatus := @myRevieverOfStrings; // where myRevieverOfStrings is ...procedure Tform.myRevieverOfStrings(str:string)begin // do something with string or ignore itend;  "string" can be replaced with a variable or a even a record.  I tend to make separate call backs for status info and for debug info for more complexes classes. if ignoring is not acceptable, you can get aggressive and showmessage(info) or add a visual indicator on your frame. MAS Thank you for ther reply, I think it is a very elegant method, now I'm try to implement it ---------------------------------------------------------------------- superc · 2022-04-21 07:29 Well, I'm implemented similar code to my program and works well, except the part: 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 TThreadComm.PassStr(Str);begin  if assigned(FOnShowStatus) then    begin      FOnShowStatus(str);    end;end; I don't understand where to insert this piece of code, why should i use assigned? Thanks in advance. ---------------------------------------------------------------------- mas steindorff · 2022-04-21 18:47 sorry, I grab the code from one of my thread objects since that is what was in front of me at the time.  Replace "TThreadComm" with your "tFrame" class name and add it to your frame code. I'll correct my example post as well. ---------------------------------------------------------------------- superc · 2022-04-22 08:09 Quote from: mas steindorff on April 21, 2022, 08:47:41 pmsorry, I grab the code from one of my thread objects since that is what was in front of me at the time.  Replace "TThreadComm" with your "tFrame" class name and add it to your frame code. I'll correct my example post as well. Thank you again, the code is perfect, I did not understand why you have used assigned: later I realized that it is used to check that FOnShowStatus is not null. ---------------------------------------------------------------------- mas steindorff · 2022-04-23 22:08 Advanced info: As you may have noted, this is a 1:1 soliton that works just fine to a (frame: parent) interface.  sometimes you want a (1:many) interface where a central class needs to pass info on to several other classes /forms or you need to direct debug information to another form than the primary one that created it.  So in affect, you need several FOnshowStatus variables.  This is where generics can help out with it's tList<> class.  there is a code snippet under the unit's type section:  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    TShowStatusEvent = procedure(status:string) of object; // like you have now   TLPassStatus = specialize TFPGList; //and then in your class OnStatus :TLPassStatus;  ...  You will need to include fgl beforehand.  then like any list, somewhere before you use it:  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   OnStatus := TLPassStatus.Create;   // like in your class create...   if assigned (OnStatus ) then      OnStatus.free;                         // in your class destroy // and then to add a client  procedure TmyClass_IF.Reg4OnShowStatus(AValue: TShowStatusEvent);begin           // to pass message from the this class to many  if not Assigned(self) then     exit;  if not Assigned(self.OnStatus) then begin     self.OnStatus := TLPassStatus.Create;     end;  self.OnStatus.Add(AValue);end; // to use procedure TmyClass_IF.PassStatusMsg(str: string);var i:Integer;begin  try    for i:= 0 to self.OnStatus.Count-1 do begin      if Assigned(self.OnStatus.Items[i]) then         self.OnStatus.Items[i](str);      end;  except     On E:Exception do  //  EConvertError        ShowErrMsg('myClass_IF: Pass status error '+ E.Message);     // else ; // if not eConvertError  end; end;  the tlist comes with a builtin search so if you need to "unregister" a function, just use self.OnStatus.remove(Avalue); //where aValue is the same function you "registered" the result can be used to verify Avalue was found and removed if >= 0 If you do use this in your tFame, do note that adding a .Destroy to tFrames can come with issues. I tend to get more than one call to my .Destroy() per frame so be sure to write it in a matter that it can be called several times. MAS