Beides im Interface-Teil geht nicht ohne zirkuläre Referenz. Die Frage ist, wieso du die Referenz im Interface-Teil brauchst. Was du tun kannst ist eine allgemeine TForm Referenz als Feld im Interface-Teil zu haben und im Implementation Teil zu casten.
Mit Class-Helper kommst du aber einigermassen Nahe an dein Ziel ran. Das ist aber reine Compiler-Trickserei und kann im Prinzip nichts, was du nicht auch über globale Funktionen erreichen könntest. Es ist reine Code-Kosmetik aber schlussendlich sieht es tatsächlich aus, als ob beide Klassen die gewünschten Kreuzreferenzen besitzen. Jedenfalls kannst du den Class Helper im Implementation-Teil einfügen und kannst so künstlich eine neue Property in TForm1/TForm2 einfügen, welche die gewünschte Referenz auf die jeweils andere Form castet. Hab unten mal ein Beispiel eingefügt, jedoch ohne den Cast sondern der Einfachheit halber mit statischer Verlinkung.
UForm1.pas
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
| unit UForm1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private public end;
var Form1: TForm1;
implementation
uses Unit1;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(Self.Form2.Caption); end;
end. |
UForm2.pas
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
| unit UForm2;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm2 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private public end;
var Form2: TForm2;
implementation
uses Unit1;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject); begin ShowMessage(Self.Form1.Caption); end;
end. |
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
| unit Unit1;
interface
uses UForm1, UForm2;
type TForm1H = class helper for TForm1 private function GetForm2: TForm2; inline; public property Form2: TForm2 read GetForm2; end;
type TForm2H = class helper for TForm2 private function GetForm1: TForm1; inline; public property Form1: TForm1 read GetForm1; end;
implementation
function TForm1H.GetForm2: TForm2; begin Result := UForm2.Form2; end;
function TForm2H.GetForm1: TForm1; begin Result := UForm1.Form1; end;
end. |