Calling Form by String

Asked

Viewed 141 times

-2

Hello, I made a routine to call the forms by the name of Form, 'fmContas', 'fmProdutos', etc., after calling it they are with NIL, how to assign the default form to the name?

The Routine: I call to chama_prog('fmProducts');

    procedure TfmMain.chama_prog(const Nome : AnsiString);
var
  FrmClass : TFormClass;
  Frm : TForm;
begin
  { Criar o Formulário pelo Nome }
  try
     FrmClass := TFormClass(FindClass('T'+Nome));

     if not prog_ativo(nome) then
        begin
        Frm := FrmClass.Create(nil);
        Frm.Name := nome;
        Frm.Show;
        end;
  except
    on E: EClassNotFound do
    begin
      MensGeral('Módulo não Encontrado ' +#13+ 'Possivelmente não foi Liberado.', 'E');
    end;
  end;
end;

function TfmMain.prog_ativo(nome: AnsiString): Boolean;
var
  i: Integer;
begin
  Result := false;
  for i := 0 to Screen.FormCount -1 do
      begin
      if Screen.Forms[i].Name = nome then
         begin
         Screen.Forms[i].BringToFront;
         Result := true;
         Exit;
         end;
      end;
end;

The Problem, in this example, fmProdutos, I have a data module, dmProdutos that in the Afterscroll Event wanted to test if fmProducts is created (not NIL).

procedure TdmProdutos.tbProdutosAfterScroll(DataSet: TDataSet);
begin
if fmProdutos <> nil then
   fmProdutos.edCodigo.Text := FloatToStrF(tbProdutosPRO_CODIGO.Value, ffNumber, 9, 0);
end;

But the fmProdutos although it’s no mistake, it’s always NIL because I actually created the FRM, someone knows how to fix it?

  • Thanks Luiz, it was the first time I used Stackoverflow to ask

1 answer

1

I ended up doing so, it worked but if anyone has a better solution, please

for i := fmMain.MDIChildCount - 1 downto 0 do
    begin
    if fmMain.MDIChildren[i].Name = 'fmProdutos' then
       fmProdutos := TfmProdutos(fmMain.MDIChildren[i]);
    end;

Browser other questions tagged

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