does anybody know how to create and insert frames at runtime ?
I would like to insert a frame in a panel, but I always get the runtime
error 'EInvalidOperation: Element 'MyFrame' has no superior window'
(this error message is translated from German, so it may differ a little
bit)
Setting the frame's visibility to false and set MyFrame.Parent := Panel
after the Create-call doesn't work either.
Any suggestions ?
Regards,
Bernhard
I thought it would be clear.
...
type
TFMain = class(TForm)
...
Panel : TPanel;
...
...
uses UMyFrame;
...
procedure TFMain.Button1Click(Sender: TObject);
var f: TFrame;
begin
f := TMyFrame.Create(Panel);
f.Parent := Panel;
end;
...
Ok, that's it.
Any ideas why it does not work ?
Regards,
Bernhard
>uses UMyFrame;
>...
>procedure TFMain.Button1Click(Sender: TObject);
>var f: TFrame;
>begin
> f := TMyFrame.Create(Panel);
> f.Parent := Panel;
>end;
>Any ideas why it does not work ?
No. It works for me:
TFrame4 = class(TFrame)
private
{ Private declarations }
public
{ Public declarations }
end;
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure TForm1.Button1Click(Sender: TObject);
var
f: TFrame4;
begin
f := TFrame4.Create(Panel1);
f.Parent := Panel1;
end;
The error is elsewhere.
Place a TListBox on your frame. It still works.
Then edit the Items property and enter some strings. Now
you'll get first an EInvalidOperation exception as described
before and then a EReadError 'Error reading ListBox1.Items.Strings:
element MyFrame has no superior window'
Why does a listbox need a window handle to read its strings ?!?
Regards,
Bernhard
f := TMyFrame.Create(self);
f.Parent := Panel;
Frames stream in the child controls on creation from their dfm-file.
VCL streaming assumes owners which are either forms or frames. So a
TPanel as an owner might mess it up.
Renate
I don't think so and BTW I tested the code I posted where I left it exactly
as it was. However, I always set the form as the owner. Besides that, In
Bernhardt's code there's no direct way to reference the frame because f is
local, which also looks strange.
I trusted that you had tried the code, but you didn't seem to have any
controls on your frame. Anyway, you are right, there's something else
in the code. I just tried it any which way, and couldn't get it to bomb
either.
Renate
I understand you mean placing TListBox at design time and editing Items at
design time. Well, I tried (I entered 2 strings) and it still works fine. I
suspect that you are withholding from us something else, like in your first
e-mail. In order to save everybody's time, boil down your problem to the
smallest compilable piece of code and then post it. And there's a big chance
that while doing it you will even find the error yourself.
That's what you said, and that's what I tried. Do you have the updated
version of D5? They might have changed some frames stuff. Your error
must be happening while the frame's dfm file is being read. You could
step into the frame's constructor in the VCL (check debug dcu's under
compiler options) and find out exactly where it happens.
Renate
>I just tried it any which way, and couldn't get it to bomb either.
>
> Renate
>
Try this:
Place a TListBox on your frame. It still works.
Then edit the Items property and enter some strings. Now
you'll get first an EInvalidOperation exception as described
before and then a EReadError 'Error reading ListBox1.Items.Strings:
element MyFrame has no superior window'
Same thing happens if you place any component with a non-empty TStrings
property on the frame.
Bernhard
Because the TListBox is a thin wrapper around the Windows object and the
latter actually holds the strings.
Mike Orriss (TeamB)
(Unless stated otherwise, my replies relate to Delphi 4.03/5.00)
(Unsolicited e-mail replies will most likely be ignored)
with items and...it worked! Then I traced into the code and found that in one case
the compiler eliminated the symbols WndParent and Style in
Classes.WinControl.CreateWnd in line 4: 'if (WndParent...' but not
in my (buggy) case. But I did NOT recompile the VCL, I have only to
directories containing the compiled files: delphi5\lib and
delphi5\lib\debug.
Anyway, after deleting all non-vital files of the project (dsm, dof,
cfg) and all dcu's, it works ! I don't know why, but who cares...
Thanx for you help,
Bernhard