![]() |
![]() |
| Paypal | FTP | CCD | Buscar | Trucos | Trabajo | Foros |
|
#5
|
||||
|
||||
|
Otra posible solución es mediante interfaces. Aunque reconozco que para un problema tan secnillo quizá sea excesivo, creo que puede servir para entender la idea.
En primer lugar definimos una interfaz que será la que deberán cumplir los forms "llamadores": Código:
unit ConRespuesta;
interface
type
IConRespuesta = interface
['{64E8D975-447E-4B48-9BA0-21E503E19258}']
procedure Responde(Value: Integer);
end;
implementation
end.
Código:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ConRespuesta, StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
FormResponde: IConRespuesta;
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
if FormResponde <> Nil then
FormResponde.Responde(2);
Close;
end;
end.
Ahora pasamos a definir los dos forms que llamarán al tercero, ambos implementarán la interfaz creada y reaccionarán de manera diferente: Código:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ConRespuesta, StdCtrls;
type
TForm1 = class(TForm, IConRespuesta)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
procedure Responde(Value: Integer);
end;
var
Form1: TForm1;
implementation
uses Unit3, Unit2;
{$R *.dfm}
{ TForm1 }
procedure TForm1.Responde(Value: Integer);
begin
ShowMessage('Form1');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TepInterf: IConRespuesta;
begin
with TForm3.Create(Self) do
begin
//asignamos al puntero nuestra interfaz
if Succeeded(QueryInterface(IConRespuesta, TepInterf)) then
FormResponde := TepInterf;
ShowModal;
Free;
end;
end;
//este método solo es para poder acceder al segundo form en el ejemplo
procedure TForm1.Button2Click(Sender: TObject);
begin
with TForm2.Create(Self) do
begin
ShowModal;
Free;
end;
end;
end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ConRespuesta, StdCtrls;
type
TForm2 = class(TForm, IConRespuesta)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure Responde(Value: Integer);
end;
implementation
uses Unit3;
{$R *.dfm}
{ TForm2 }
procedure TForm2.Responde(Value: Integer);
begin
ShowMessage('Form2');
end;
procedure TForm2.Button1Click(Sender: TObject);
var
TepInterf: IConRespuesta;
begin
with TForm3.Create(Self) do
begin
if Succeeded(QueryInterface(IConRespuesta, TepInterf)) then
FormResponde := TepInterf;
ShowModal;
Free;
end;
end;
end.
Creo que este tipo de mecanismo puede ser muy potente en casos más complejos.
__________________
E pur si muove |
| Herramientas | Buscar en Tema |
| Desplegado | |
|
|
|