How to create form dynamically in Delphi

Asked

Viewed 1,891 times

0

Well I’m having trouble creating the form in Delphi, I’m using a procedure but it’s giving direct error.

Could someone tell me a more efficient way to create a form in Delphi?

Current code:

procedure TLogin.CriarForm(NomeForm: TFormClass);
begin
  //Procedimento para criar formulario na memória
  with NomeForm do begin
    Try begin 
      TForm(NomeForm):=NomeForm.Create(Application); 
      TForm(NomeForm).Showmodal;
    end;

    Finally
      FreeAndNil(NomeForm);
    End; 
  end;
end;
  • vixi man the Delphi xe5 n speech only points to the parameter qnd calls the Creatform(frmPrincipal) function; even with quotes from the error

  • By your code, you have to inform the form class, for example: CriarForm(TfrmPrincipal) repair the T at first.

  • blz I’ll try to group it

  • msm error dnv...

  • I tested here and it worked... Not knowing which error appears is hard to help. :/

  • let me see if anything pops up down here in the bug fixes

  • undeclared Identifier seems to have q declare the form somewhere

  • In Uses you put the Unit form?

  • I put a uses there on top of the data module frmPrincipal n puis pa automatically create qnd compiles to create dynamically afterwards

  • put in uses agr and gave msm error

  • ae agr was missing a point

  • CriarForm(frmSistema.TPrincipal);

  • blz vlw I’ll take a look at her

  • Gabriel, if possible edit the question and put the error that was presented! If you prefer post an answer with the error resolution. :)

Show 9 more comments

2 answers

1


The error in your code is in:

procedure TLogin.CriarForm(NomeForm: TFormClass);
begin
  //Procedimento para criar formulario na memória
  with NomeForm do begin
    Try begin //<- Não se abre um bloco begin-end dentro de blocos try-finally
      TForm(NomeForm):=NomeForm.Create(Application); 
      TForm(NomeForm).Showmodal;
    end; 

    Finally
      FreeAndNil(NomeForm);
    End; 
  end;
end;

The correct form would be:

procedure TLogin.CriarForm(NomeForm: TFormClass);
begin
  //Procedimento para criar formulario na memória
  with NomeForm do 
  begin
    Try
      TForm(NomeForm):=NomeForm.Create(Application); 
      TForm(NomeForm).Showmodal;
    Finally
      FreeAndNil(NomeForm);
    End; 
  end;
end;

0

If "Tformclass" is a class reference, the code can look like this:

procedure TLogin.CriarForm(NomeForm: TFormClass);
var
  form: TForm;
begin
  form := NomeForm.Create(Application);
  try
    form.ShowModal; 
  finally
    form.Free;
  end;
end;

In this case, Nomeform would be a reference to the class of Tform or descendant to be created. P.ex.: type TFormClass = class of TForm; ...

Criaform(Tform1); //creates and displays a Tform1 instance Criaform(Tform2); //creates and displays a Tform2 instance ...

  • Try to explain what you are doing in the code so that this answer is more tangible to future users and to the owner of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.