Recent

Author Topic: Can I safely implement `TFrame.Release`?  (Read 1209 times)

piola

  • Full Member
  • ***
  • Posts: 145
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Can I safely implement `TFrame.Release`?
« on: July 27, 2024, 03:07:48 pm »
Dear all,

unfortunately, TFrame doesn't implement the Release method as TForm does. TForm.Release does nothing more than

Code: [Select]
procedure TCustomForm.Release;
begin
  if Application <> nil then
    Application.ReleaseComponent(Self)
  else
    Free;
end;

As TFrame is also a TComponent descendant, I'd like to add the same Release method to my frames.

But I wonder whether I can do this safely because I assume that there's a good reason that TFrame lacks the Release method.

jamie

  • Hero Member
  • *****
  • Posts: 6515
Re: Can I safely implement `TFrame.Release`?
« Reply #1 on: July 27, 2024, 03:35:26 pm »
I don't think that is a good idea since that is only time delay free operation, but a Tframe is a child control of a form.

Unless you have your terminologies mixed up with other tools like MFC CframeWnd etc.

if you are referring to a TFORM, then yes it should be ok.
The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11724
  • FPC developer.
Re: Can I safely implement `TFrame.Release`?
« Reply #2 on: July 27, 2024, 03:54:05 pm »
(note that disconnecting frames from forms, and e.g. moving to another is a bad idea.  Some components like Tmenu might hook into some global list to get events. At least it does in delphi)

piola

  • Full Member
  • ***
  • Posts: 145
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: Can I safely implement `TFrame.Release`?
« Reply #3 on: July 27, 2024, 05:43:48 pm »
Well, I use frames to display different content on a TPanel. All I want to do is to free the panel if it's not needed anymore. But this situation can also occur if the user clicks the "close" button of the frame. And I cannot free the frame inside one of its event handlers. That's why I'd like to use Release.

cdbc

  • Hero Member
  • *****
  • Posts: 1497
    • http://www.cdbc.dk
Re: Can I safely implement `TFrame.Release`?
« Reply #4 on: July 27, 2024, 07:14:58 pm »
Hi
Use 'PostMessage' from 'LCLIntf' and LM_USER from 'LMessages', then you simply send a message to the ParentForm.Handle, e.g.:
Code: Pascal  [Select][+][-]
  1. const
  2.   LM_FRAMECLOSE = LM_USER + 5;
  3. ...
  4. // in ParentForm:
  5. ...
  6.   public
  7.     procedure LMFrameClose(var Message: TLMessage); message LM_FRAMECLOSE;
  8. ...
  9. implementation
  10.  
  11. Procedure TForm1.LMFrameClose(var Message: TLMessage);
  12. begin
  13.   TFrame(Message.WParam).Free; // you can do checks if you like...
  14. end;
  15.  
  16. ////////////////// and in your frame's close button do
  17. procedure TMyFrame.btnCloseClick(Sender: TObject);
  18. begin
  19.   PostMessage(Form1.Handle,LM_FRAMECLOSE,ptrint(Self),-1);
  20. end;
  21.  
This way you avoid calling free in its own eventhandler...
Untested, just written here...  %)
Try it and see if it works for you.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

piola

  • Full Member
  • ***
  • Posts: 145
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: Can I safely implement `TFrame.Release`?
« Reply #5 on: July 27, 2024, 08:40:34 pm »
Thank you. If it's recommended to prefer it to "Release", I'll do so.  :)

Joanna

  • Hero Member
  • *****
  • Posts: 981
Re: Can I safely implement `TFrame.Release`?
« Reply #6 on: July 27, 2024, 11:09:53 pm »
Well, I use frames to display different content on a TPanel. All I want to do is to free the panel if it's not needed anymore. But this situation can also occur if the user clicks the "close" button of the frame. And I cannot free the frame inside one of its event handlers. That's why I'd like to use Release.
I have used frames a lot but I’ve never never heard of releasing them.
Why not have the button that closes the frame located in the parent of frame? Or Is there a way to call the event handler of frame’s parent to free it?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

MarkMLl

  • Hero Member
  • *****
  • Posts: 7453
Re: Can I safely implement `TFrame.Release`?
« Reply #7 on: July 27, 2024, 11:45:05 pm »
I have used frames a lot but I’ve never never heard of releasing them.
Why not have the button that closes the frame located in the parent of frame? Or Is there a way to call the event handler of frame’s parent to free it?

I've used them a lot in the context of notebook tabs, without having given much thought to what is actually happening when a tab/page containing a frame is deleted.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Joanna

  • Hero Member
  • *****
  • Posts: 981
Re: Can I safely implement `TFrame.Release`?
« Reply #8 on: July 28, 2024, 12:00:33 am »
I’m not familiar with notebook butI’ve used the frames inside of tcdpagecontrol and the frame is deleted when the page is destroyed. The funny thing is it was mistaking the page being destroyed as a page change event and I had to add extra code to check if the page was csdestroying.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

cdbc

  • Hero Member
  • *****
  • Posts: 1497
    • http://www.cdbc.dk
Re: Can I safely implement `TFrame.Release`?
« Reply #9 on: July 28, 2024, 02:18:25 am »
Hi
I've put together a little project, that shows 2 methods of doing what you're after:
1) method 1 uses 'PostMessage' to communicate
2) method 2 uses 'Application.QueueAsyncCall' to communicate
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

piola

  • Full Member
  • ***
  • Posts: 145
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: Can I safely implement `TFrame.Release`?
« Reply #10 on: July 28, 2024, 11:01:59 am »
Thank you all!  :)

egsuh

  • Hero Member
  • *****
  • Posts: 1436
Re: Can I safely implement `TFrame.Release`?
« Reply #11 on: July 30, 2024, 07:05:52 am »
Why not free? I made a simple demo projects and works fine.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.    Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.    { TForm1 }
  13.  
  14.    TForm1 = class(TForm)
  15.       Button1: TButton;
  16.       procedure Button1Click(Sender: TObject);
  17.       procedure FormCreate(Sender: TObject);
  18.    private
  19.  
  20.    public
  21.  
  22.    end;
  23.  
  24. var
  25.    Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. uses unit2;
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. begin
  37.    Self.Free;
  38. end;
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.    TFrame1.Create(self).parent := Self;
  43. end;
  44.  
  45. end.
  46.  
  47. /////////////////////////// Frame unit  ///////////////////////////////
  48.  
  49. unit Unit2;
  50.  
  51. {$mode ObjFPC}{$H+}
  52.  
  53. interface
  54.  
  55. uses
  56.    Classes, SysUtils, Forms, Controls, StdCtrls;
  57.  
  58. type
  59.  
  60.    { TFrame1 }
  61.  
  62.    TFrame1 = class(TFrame)
  63.       Button1: TButton;
  64.       procedure Button1Click(Sender: TObject);
  65.    private
  66.  
  67.    public
  68.  
  69.    end;
  70.  
  71. implementation
  72.  
  73. {$R *.lfm}
  74.  
  75. { TFrame1 }
  76.  
  77. procedure TFrame1.Button1Click(Sender: TObject);
  78. begin
  79.    Self.Free;
  80. end;
  81.  
  82. end.
  83.  

 

TinyPortal © 2005-2018