It is called in Create. (There is no OnCreate, as the component meant to be the parent is not a form)
Main Topics
Browse All TopicsI want to embed a form within a component I am writing. Right now, the following code is in my Create:
FForm := TTheForm.Create(self);
FForm.Hide;
FForm.BorderStyle := bsNone;
FForm.Parent := self;
FForm.Align := alClient;
FForm.Show;
As soon as FForm.Show is called, I get the "Control '' has no parent window" exception.
I figure the constructor is a bad place to call "Show". What would be the right place?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I've done a similar thing for a replacement for the TDateTimePicker
in the Create I dont set a parent at all
so
FDrop:=TForm.Create(Self);
FDrop.BorderStyle:=bsNone;
FDrop.FormStyle:=fsStayOnT
FDrop.ClientHeight:=170;
FDrop.ClientWidth:=200;
FDrop.Name:='SDatePickForm
FDrop.OnKeyDown:=GetDropKe
most of the lines are irrelevant so ignore them
but I just do a show as in
FDrop.Show;
everything works fine
regards
Actually, it is necessary that I set the parent.
The reason I want to use a form in this way is because I have a bunch of buttons and other controls that need to appear inside each instance of this component. Rather than take the tedious route of creating them all dynamically, I'd rather do it in the form designer and then "mount" this form on the client area of the component.
(I've had no problems in the past mounting forms on other components, but in those cases, the components were already created and visible. I am assuming that inside the constructor of a TWinControl, the control is not yet showing so this is what causes the exception...)
Yes.
Here is the code in its complete context (minus the unrelated bits...):
constructor TDetailColumn.Create(AOwne
begin
inherited;
FForm := TBrowserForm.Create(self);
FForm.Hide;
FForm.BorderStyle := bsNone;
FForm.Parent := self;
FForm.Align := alClient;
FForm.Show;
end;
I'm pretty sure I understand why the exception is happening, but can't think of any other place I can put FForm.Show...
try this
constructor TDetailColumn.Create(AOwne
begin
inherited Create(AOwner); //concrete the inherited
FForm := TBrowserForm.Create(self);
FForm.Hide;
FForm.BorderStyle := bsNone;
FForm.Parent := self;
FForm.Align := alClient;
//FForm.Show; //leave this out, not sure, but the embedded form should be shown if the parent is shown
end;
> FForm.Parent := Application;
is not a solution, because Application is an invisible form
meikl ;-)
That would be great! To save you some time, I created a barebones minimum project that reproduces this. If you want, I can email it to you as well.
(For the sake of brevity, I didn't include the code for SmallFrm here since it is just a form with a button and no code.)
--------------------------
unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SmallFrm, StdCtrls, ExtCtrls;
type
TTestComponent = class(TCustomPanel)
protected
FForm :TSmallForm;
public
constructor Create(AOwner :TComponent); override;
end;
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FTest :TTestComponent;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
{ TTestComponent }
constructor TTestComponent.Create(AOwn
begin
inherited;
FForm := TSmallForm.Create(self);
FForm.Hide;
FForm.BorderStyle := bsNone;
FForm.Parent := self;
FForm.Align := alClient;
FForm.Show;
end;
procedure TMainForm.Button1Click(Sen
begin
if not assigned(FTest) then begin
FTest := TTestComponent.Create(self
FTest.Align := alRight;
FTest.Left := Width - 300;
FTest.Parent := self;
end;
end;
end.
yes of course your panel did not have a parent
a workaround could be
constructor TTestComponent.Create(AOwn
begin
inherited;
FForm := TForm2.Create(self); //had to recode a bit ;-))
FForm.BorderStyle := bsNone;
FForm.Parent := self;
FForm.Align := alClient;
if (AOwner is TWinControl) then
begin
parent := TWinControl(AOwner);
FForm.Show;
end;
(* mabe additional
else raise Exception.Create('The Owner must be a Descandend of TWinControl');
*)
end;
meikl ;-)
Business Accounts
Answer for Membership
by: kretzschmarPosted on 2006-04-05 at 11:09:01ID: 16384519
guess you call this in the oncreate of self, or?