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

質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top