views:

96

answers:

1

I am trying to convert a set of forms that were created with a custom tool to Delphi forms. I am trying to add all the necessary components at runtime and then use WriteComponentResFile to create the DFM file.

All of my initial tests looked good until I tried adding a TPageControl and TabSheets. The current forms can have multiple pages so I was going to mirror this using the PageControl. The problem is any components I add to a TabSheet are not streamed out to the DFM. It looks good if I show the form but something is missing for WriteComponentResFile.

I am writing out a corresponding pas file so I can open this up in the IDE once they are done. The goal is to move away from the custom form designer and start to use the Delphi IDE for our form designer.

Here is some sample code showing how I am creating the components:

procedure WriteFormAsDFM(OutputFileName: string);
var
  PageIndex: integer;
  PageCount: Integer;
  OutputForm: TForm;
  Pages: TPageControl;
  NewPage: TTabSheet;
  NewLabel: TLabel;
begin

  OutputForm := TForm.Create(nil);
  OutputForm.Name := ChangeFileExt(ExtractFileName(OutputFileName), '');
  OutputForm.Caption := OutputForm.Name;
  OutputForm.Height := 300;
  OutputForm.Width := 300;

  Pages := TPageControl.Create(OutputForm);
  Pages.Parent := OutputForm;
  Pages.Top := 50;
  Pages.Left := 0;
  Pages.Height := 200;
  Pages.Width := 200;

  NewLabel := TLabel.Create(OutputForm);
  NewLabel.Parent := OutputForm;
  NewLabel.Caption := 'Label on Form';

  //write pages
  PageCount := 2;

  for PageIndex := 0 to PageCount - 1 do
  begin
    NewPage := TTabSheet.Create(Pages);
    NewPage.Parent := Pages;
    NewPage.PageControl := Pages;
    NewPage.Caption := 'Page ' + IntToStr(PageIndex);
    NewPage.Name := 'tsPage' + IntToStr(PageIndex);

    NewLabel := TLabel.Create(NewPage);
    NewLabel.Parent := NewPage;
    NewLabel.Caption := 'Label on ' + NewPage.Caption;
  end;

  WriteComponentResFile(OutputFileName, OutputForm);
  //WritePasFile(OutputFileName, OutputForm);

  OutputForm.ShowModal;

  FreeAndNil(OutputForm);
end;

and here is the DFM file that is output. You can see the label on the form is created but not the labels added to the TabSheets.

object Form123: TForm
  Left = 69
  Top = 69
  Caption = 'Form123'
  ClientHeight = 264
  ClientWidth = 284
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object TLabel
    Left = 0
    Top = 0
    Width = 67
    Height = 13
    Caption = 'Label on Form'
  end
  object TPageControl
    Left = 0
    Top = 50
    Width = 200
    Height = 200
    ActivePage = tsPage0.Owner
    TabOrder = 0
    object tsPage0: TTabSheet
      Caption = 'Page 0'
      ExplicitLeft = 0
      ExplicitTop = 0
      ExplicitWidth = 0
      ExplicitHeight = 0
    end
    object tsPage1: TTabSheet
      Caption = 'Page 1'
      ExplicitLeft = 0
      ExplicitTop = 0
      ExplicitWidth = 0
      ExplicitHeight = 0
    end
  end
end
+5  A: 

Try to use the form as owner of the components.

NewPage := TTabSheet.Create(OutputForm);

NewLabel := TLabel.Create(OutputForm);

Uwe Raabe
Thanks that took care of it.
Mark Elder