Search code examples
formsobjectdelphidynamicrichedit

Delphi - Rich edit doesn't show on dynamically created form


I've dynamically created a Form in my program, and it works and shows perfectly, but the RichEdit I've also dynamically created doesn't want to show on the Form at all. How can I show the RichEdit on the Form?

Code I'm using:

procedure TfrmPuntehou.lblAbbClick(Sender: TObject);
var
  frmAbb: TForm;
  redAbbreviations: TRichEdit;
begin
  //opens abbreviations
  frmAbb := TForm.Create(nil);
  redAbbreviations := TRichEdit.Create(nil);
  try
    with frmAbb do
    begin
      Width := 400;
      Height := 400;
      Caption := 'Abbreviations';
      Position := poOwnerFormCenter;
      ShowModal;
    end;
    with redAbbreviations do
    begin
      Parent := frmAbb;
      Width := 300;
      Height := 353;
      redAbbreviations.Paragraph.TabCount := 2;
      redAbbreviations.Paragraph.Tab[0] := 30;
      redAbbreviations.Paragraph.Tab[1] := 60;
      Lines.Add('DEV'+#9+'='+#9+'SWD Development');
      Lines.Add('1660'+#9+'='+#9+'1660s');
      Lines.Add('2.1'+#9+'='+#9+'2.1s');
      Lines.Add('MIN'+#9+'='+#9+'Minis');
      Lines.Add('SR'+#9+'='+#9+'Stockrods');
      Lines.Add('PR'+#9+'='+#9+'Pinkrods');
      Lines.Add('HR'+#9+'='+#9+'Hotrods');
      Lines.Add('HM'+#9+'='+#9+'Heavy Metals');
      Lines.Add('V8'+#9+'='+#9+'V8s');
      Lines.Add('MA'+#9+'='+#9+'Midgets A');
      Lines.Add('MB'+#9+'='+#9+'Midgets B');
      Lines.Add('SP'+#9+'='+#9+'Sprints');
      Lines.Add('CRO'+#9+'='+#9+'Crosskarts');
      Lines.Add('LM'+#9+'='+#9+'Late Models');
      Font.Size := 13;
    end;
  finally
    frmAbb.Free;
  end;
end;

Solution

  • Move the ShowModal from the initialization part of the frmAbb to the end of the code, just before the finally statement.

    procedure TForm1.Button1Click(Sender: TObject);
    var
      frmAbb: TForm;
      redAbbreviations: TRichEdit;
    begin
      //opens abbreviations
      frmAbb := TForm.Create(nil);
      try
        redAbbreviations := TRichEdit.Create(frmAbb);
        with frmAbb do
        begin
          Width := 400;
          Height := 400;
          Caption := 'Abbreviations';
          Position := poOwnerFormCenter;
        end;
        with redAbbreviations do
        begin
          Parent := frmAbb;
          Width := 300;
          Height := 353;
          redAbbreviations.Paragraph.TabCount := 2;
          redAbbreviations.Paragraph.Tab[0] := 30;
          redAbbreviations.Paragraph.Tab[1] := 60;
          Lines.Add('DEV'+#9+'='+#9+'SWD Development');
          Lines.Add('1660'+#9+'='+#9+'1660s');
          Lines.Add('2.1'+#9+'='+#9+'2.1s');
          Lines.Add('MIN'+#9+'='+#9+'Minis');
          Lines.Add('SR'+#9+'='+#9+'Stockrods');
          Lines.Add('PR'+#9+'='+#9+'Pinkrods');
          Lines.Add('HR'+#9+'='+#9+'Hotrods');
          Lines.Add('HM'+#9+'='+#9+'Heavy Metals');
          Lines.Add('V8'+#9+'='+#9+'V8s');
          Lines.Add('MA'+#9+'='+#9+'Midgets A');
          Lines.Add('MB'+#9+'='+#9+'Midgets B');
          Lines.Add('SP'+#9+'='+#9+'Sprints');
          Lines.Add('CRO'+#9+'='+#9+'Crosskarts');
          Lines.Add('LM'+#9+'='+#9+'Late Models');
          Font.Size := 13;
        end;
        frmAbb.ShowModal;
      finally
        frmAbb.Free;
      end;
    end;