9

I often embed a TForm descendant into another TForm descendant like this:

var
  Form1: TForm1;
  Form2: TForm2;
begin
  Form2.Parent      := Form1;
  Form2.BorderStyle := bsNone;
  Form2.Align       := alClient;
  Form2.Show;
end;

Usually this works just fine, but sometimes the controls in Form2 are not aligned properly. Is there a general workaround for this sort of problem?

Does anybody know what is causing this "misalignment"?

I know that I could use TFrame for this kind of job, but I have a lot of library code that I would have to rewrite and I do not see any reason why the TForm in TForm approach should not work?

Edit: I have identified the component TcxListView as the culprit here, I have submitted a bug report to the component vendor (DevExpress):

http://www.devexpress.com/issue=B194161

Edit 2: The developers at DevExpress have analyzed the problem and said that it is actually a defect in the TGridPanel component by Embarcadero:

http://qc.embarcadero.com/wc/qcmain.aspx?d=90324

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113

2 Answers2

8

I do this as well and I use the following routine to make it happen:

procedure TMyForm.PlaceInsideContainer(Container: TWinControl);
begin
  Parent := Container;
  Align := alClient;
  BorderIcons := [];
  BorderStyle := bsNone;
  ParentBackground := True;
  Show;
end;

I have no problems with this. The only difference that I could possibly imagine could be relevant is the assignment of BorderIcons, but I would doubt that causes a problem.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    Is there any reason to set the `BorderIcons` to `[]` even when the `BorderStyle` is `bsNone`? – Jens Mühlenhoff Dec 08 '10 at 10:24
  • @Jens Well, this code is so old I can't remember why it is the way it is. I wonder if setting BorderIcons to [] means that the ALT+SPACE, ALT+F4 shortcuts become disabled. – David Heffernan Dec 08 '10 at 10:48
  • I have no Delphi VCL/RTL source code or Delphi compiler available here, but I think that `BorderIcons` has no effect if `BorderStyle` is `bsNone`. In that case, Alt+Space doesn't work but All+F4 does. – Andreas Rejbrand Dec 08 '10 at 11:21
  • Yeah, I just did some testing and I think that setting BorderIcons is pointless. Maybe it stems from some old version of Delphi or more likely I was just having a bad day! – David Heffernan Dec 08 '10 at 11:29
1

I read a similar question (you'll have to google it) and the answer from TeamB was not to do this as the behaviour was unpredictable and that you should use TFrame instead (which is what I have always done).

Mike Versteeg
  • 1,615
  • 14
  • 28
  • 1
    I have FAR MORE problems using TFrame than with docking one form on another. – Warren P Dec 09 '10 at 00:41
  • Hosting a form inside another form is essential to all of my apps. – Brian Frost Dec 15 '10 at 09:47
  • 1
    Consider placing a TPanel on the container TForm and use it as a DockSite for docking embedded TForm objects. TForm really is not well-designed to be set as a direct child of another TForm. – Remy Lebeau Jan 06 '11 at 01:54