Why doesn't my run-time-created component appear on the form?
delphi
Solution
You need to set `Parent` in the runtime code.
MyPanel2 := TMyPanel.Create(Self);
with MyPanel2 do
begin
Parent := Self;//oops, you forgot to set this
SetBounds(10, 10, 400, 400);
Image.Picture.LoadFromFile('C:\test.png');
end;
The code in your question won't result in the control showing for a plain vanilla `TPanel`, or indeed any control.
From the documentation, with my emphasis:
Specifies the parent of the control.
Use the Parent property to get or set the parent of the control. The parent of a control is the control that contains it. For example, if an application includes three radio buttons in a group box, the group box is the parent of the three radio buttons, and the radio buttons are the child controls of the group box.
To serve as a parent, a control must be an instance of a TWinControl descendant.
When creating a new control at run time, assign a Parent property value for the new control. Usually, this is a form, panel, group box, or a control that is designed to contain another. Changing the parent of a control moves the control onscreen so that it is displayed within the new parent. When the parent control moves, the child moves with the parent.
Problem
I am testing the example that came from this Q&A Component Creation - Joining Components Together? to learn how to create a custom/composite component. While the installed component from the example works dragging on to the form, I can't seem to create it at run time. ``` procedure TForm1.Button1Click(Sender: TObject); var MyPanel2 : TMyPanel; begin MyPanel2 := TMyPanel.Create(Form1); With MyPanel2 do begin Left := 10; Top := 10; Width := 400; Height := 400; Visible := True; Image.Picture.LoadFromFile('C:\test.png'); end; end; ``` I tried both self and Form1 as the owner. Played with properties of both the panel and the image. Just not sure what I am doing wrong. No errors except when I forgot to add pngimage to my uses. Steps through the code just fine, nothing visually occurs for the run time creation.