Executando um método[function] pelo nome
Como executar uma procedure pelo nome? Um modo de como executar um método criado em um form, em outro form!
unit USistema;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFSistema = class(TForm)
BtnChamaOutraTela: TButton;
procedure BtnChamaOutraTelaClick(Sender: TObject);
procedure RecalcularParcelas(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FSistema: TFSistema;
implementation
uses UProdutos;
{$R *.dfm}
procedure TFSistema.BtnChamaOutraTelaClick(Sender: TObject);
begin
FProdutos := TFProdutos.Create(Self);
try
FProdutos.Form_Pai := FSistema;
FProdutos.ShowModal;
finally
FProdutos.Free;
end;
end;
procedure TFSistema.RecalcularParcelas(Sender: TObject);
begin
ShowMessage('Recalculado!');
end;
end.
Segunda tela
unit UProdutos;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFProdutos = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure ExecMethod(OnObject: TObject; MethodName: string);
public
Form_Pai: TForm;
{ Public declarations }
end;
var
FProdutos: TFProdutos;
type
TExec = procedure of object;
implementation
{$R *.dfm}
{ TFProdutos }
procedure TFProdutos.Button1Click(Sender: TObject);
begin
if Form_Pai <> nil then
ExecMethod(Form_Pai, 'RecalcularParcelas');
end;
procedure TFProdutos.ExecMethod(OnObject: TObject; MethodName: string);
var
Routine: TMethod;
Exec: TExec;
begin
Routine.Data := Pointer(OnObject);
Routine.Code := OnObject.MethodAddress(MethodName);
if not Assigned(Routine.Code) then
Exit;
Exec := TExec(Routine);
Exec;
end;
end.
Exemplo: Download do exemplo
Muito bom, deu certo
ResponderExcluir