Why doesn't my new control appear if I pass the parent control to Create instead of assigning the Parent property?

StackOverflow https://stackoverflow.com/questions/13843874

Question

In Lazarus I'm trying this:

TabSaveButton := TButton.Create(nil);
with TabSaveButton do
  begin
    Parent:=NewTab;
    Width:=75;
    Height:= 25;
    Top:=530;
    Left:=715;
    Caption:='Save';
  end;

And it works. I.e., I get the button and it's clickable, and it is the child of a dynamically created tab sheet.

But the following does not show the button, nor errors:

TabSaveButton := TButton.Create(NewTab);
with TabSaveButton do
  begin
    Width:=75;
    Height:= 25;
    Top:=530;
    Left:=715;
    Caption:='Save';
  end;
  1. Why does the second method not work?

  2. Is this the same effect on both Lazarus and Delphi?

Était-ce utile?

La solution

The argument of Create sets the owner of the control. The owner is the component responsible for freeing the component in question. For instance, if you free a component, then all components owned by it are also freed. The parent is a completely different thing. It is the window (control) hosting the control in question.

There is no difference between Delphi and Lazarus here.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top