1

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
PrimeBeat
  • 444
  • 5
  • 15

2 Answers2

4

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;
fpiette
  • 11,983
  • 1
  • 24
  • 46
Mark
  • 422
  • 2
  • 10
1

You forgot to make it visible:

redAbbreviations.Visible := TRUE;

And you show the form modal before setting properties to the RichEdit.

Here is the correct reformatted code:

procedure TForm1.Button1Click(Sender: TObject);
var
    frmAbb           : TForm;
    redAbbreviations : TRichEdit;
begin
    // opens abbreviations
    frmAbb := TForm.Create(nil);
    try
        redAbbreviations := TRichEdit.Create(frmAbb);
        frmAbb.Width                        := 400;
        frmAbb.Height                       := 400;
        frmAbb.Caption                      := 'Abbreviations';
        frmAbb.Position                     := OwnerFormCenter;
        redAbbreviations.Parent             := frmAbb;
        redAbbreviations.Width              := 300;
        redAbbreviations.Height             := 353;
        redAbbreviations.Paragraph.TabCount := 2;
        redAbbreviations.Paragraph.Tab[0]   := 30;
        redAbbreviations.Paragraph.Tab[1]   := 60;
        redAbbreviations.Lines.Add('DEV'+#9+'='+#9+'SWD Development');
        redAbbreviations.Lines.Add('1660'+#9+'='+#9+'1660s');
        redAbbreviations.Lines.Add('2.1'+#9+'='+#9+'2.1s');
        redAbbreviations.Lines.Add('MIN'+#9+'='+#9+'Minis');
        redAbbreviations.Lines.Add('SR'+#9+'='+#9+'Stockrods');
        redAbbreviations.Lines.Add('PR'+#9+'='+#9+'Pinkrods');
        redAbbreviations.Lines.Add('HR'+#9+'='+#9+'Hotrods');
        redAbbreviations.Lines.Add('HM'+#9+'='+#9+'Heavy Metals');
        redAbbreviations.Lines.Add('V8'+#9+'='+#9+'V8s');
        redAbbreviations.Lines.Add('MA'+#9+'='+#9+'Midgets A');
        redAbbreviations.Lines.Add('MB'+#9+'='+#9+'Midgets B');
        redAbbreviations.Lines.Add('SP'+#9+'='+#9+'Sprints');
        redAbbreviations.Lines.Add('CRO'+#9+'='+#9+'Crosskarts');
        redAbbreviations.Lines.Add('LM'+#9+'='+#9+'Late Models');
        redAbbreviations.font.Size :=13;
        redAbbreviations.Visible   := TRUE;
        frmAbb.ShowModal;
    finally
        frmAbb.Free;
    end;
end;
fpiette
  • 11,983
  • 1
  • 24
  • 46
  • that's the problem, I did try that before but it still didn't show. So I removed it... – PrimeBeat Jan 25 '21 at 06:52
  • You also misplaced Showmodal. I edited my answer with correct reformatted code. – fpiette Jan 25 '21 at 07:12
  • @fpiette the 2nd `try/finally` is not needed if the Form is made to be the Owner of the RichEdit. – Remy Lebeau Jan 25 '21 at 07:21
  • @RemyLebeau But I made it nil as the OP made it. So try/finally required. – fpiette Jan 25 '21 at 07:23
  • @fpiette you re-wrote the code to remove the `with` blocks, so why not re-write it further to also assign an Owner, too? It is the correct thing to do in code like this. The lifetime of the RichEdit is being tied to the lifetime of the Form it is being displayed on, so the destruction should be automated by the Form itself. – Remy Lebeau Jan 25 '21 at 07:26
  • 1
    @RemyLebeau Edited my code according to your comment. – fpiette Jan 25 '21 at 08:01