Delphi Treeview with BD link

0

Good afternoon, I have created a treeview with the data that is in a table in the DB, and has a field that contains the name in the form. How to do to give a showmodal in the onclick event of the Treeview getting the name of the form that is in the table field?

follow the code

procedure CriaFormpeloNome(const FrmNome : string);
var
  FrmClass : TFormClass;
  Frm : TForm;
begin

  try
    FrmClass := TFormClass(FindClass(FrmNome));
    Frm      := FrmClass.Create(application);
  except
    //não achou a classe
    showmessage('Nao Achou a classe');
  end;
end;


procedure TFormFrontend.TreeView2DblClick(Sender: TObject);
var
Form2 : string;
begin
zmenu.SQL.Clear;
zmenu.SQL.Add('Select * from tab_menu');
zmenu.SQL.Add('WHERE id =:id');
zmenu.Params.ParamByName('id').value:= TreeView2.Selected.AbsoluteIndex;
zmenu.Open;
Form2:= zmenuarquivo.AsString;
Form2 := concat('T',Form2);
CriaFormpeloNome(Form2);
end;
    
asked by anonymous 17.04.2018 / 20:08

1 answer

0

The only thing I see missing from your code is that in order to use FindClass it is necessary to call RegisterClass in advance. I would say that you have to initially call the RegisterClass for all the classes of forms you want to open, and then the code that works.

This small example works:

procedure CriaFormpeloNome(const FrmNome : string);
var
   FrmClass : TFormClass;
   Frm : TForm;
begin
   try
      FrmClass := TFormClass(FindClass(FrmNome));
      Frm      := FrmClass.Create(application);
      if Frm<>nil then
        frm.ShowModal;
   except
     //não achou a classe
     showmessage('Nao Achou a classe');
   end;
end;

procedure TForm5.BitBtn1Click(Sender: TObject);
begin
  RegisterClass(TForm5);
  CriaFormpeloNome('TForm5');
end;
    
18.04.2018 / 12:29