tags:

views:

163

answers:

2

Hello

To avoid the more complicated solution where the frame calls some routine in the parent form so that the form can kill the frame... I was wondering if it's OK to simply set the form as the frame's parent, and let Delphi call Frame.Free when the user closes the application?

procedure TForm1.FormShow(Sender: TObject);
var
  Frame2 : TFrame2;
begin
  //Frame2 := TFrame2.Create(nil);
  Frame2 := TFrame2.Create(Self);
  Frame2.Align := alClient;
  Frame2.Parent := Self;
  Frame2.Visible := True;
end;

Thank you.

+10  A: 

Actually you are confusing parent and owner:

The owner is passed as parameter to the constructor and will take care of freeing the component, the parent is the control which contains the control visually.

Example:

You have got a form, a panel on that form and a label on that panel: The form usually is the owner of the panel and the label. The form is the parent of the panel and the panel is the parent of the label.

As for your question: It is perfectly OK to pass the form that contains the frame as the owner. When the form is freed, it will also free the frame. In addition you must set the parent to some other control for the frame to become visible. That can of course also be the form, but this will not have any effect on freeing the frame.

dummzeuch
+1 Great answer!
Jeroen Pluimers
Thanks, but in the case above where the frame is displayed directly on the form instead of through a Panel or a PageControl... isn't the owner and the parent is the Form in both cases? In this exemple, I assume then that the frame will be freed from RAM when the application exists.I just wanted to check that I can use this simpler solution.
OverTheRainbow
Yes, if both Parent and Owner are the form the form in its role of Owner will free the frame.
dummzeuch
Thanks for the tip.
OverTheRainbow
+1  A: 

To be crystal clear:
- The Owner is responsible for the existence of its owned Components and frees them when it destroys itself (they are part of the owner and cannot exist without it).
- The Parent is in charge of showing its children (Controls - without a Parent their Visible property has no effect) and as such will also free its Controls when it destroys itself because nobody would be able to show them anymore.

François