![]() |
operator is und as
ich habe folgendes Problem
FormA mit Property Blabla FormB mit Property Blabla Beide Formulare rufen FormC auf, in der das property Blabla gesetzt wird mit dem "is" und "as" Operator bzw. "self.owner" kann ich zwar festellen welchem formular ich das Property fülle aber ich suche nach einer eleganteren Lösung als die folgende: in FormC: if self.owner is TFormA then with self.owner as TFormA do begin BlaBla := irgendwas; ... end; if self.owner is TFormB then with self.owner as TFormB do begin BlaBla := irgendwas; ... end; Kennt jemand eine elegantere Möglichkeit? mfg DelphiDeveloper |
Re: operator is und as
Hi,
versuche doch
Delphi-Quellcode:
du musst schon den Sender übergeben.
procedure FormC.SetBLABLA( Sender: TObject; xxxx ? );
begin if Sender is FormA then begin with Sender as FormA do begin end; end else if Sender is FormB then with Sender as FormB do begin end; end;
Delphi-Quellcode:
Ich hoffe es Hilft.
FormA ...
FormC.SetBLABLA( Self, xxxx ? mfg Rumpi |
Re: operator is und as
Danke für die schnelle Antwort aber ob nun
"self.owner" oder "sender" die haessliche if Verzweigung bleibt. Es muss doch über ein typecasting irgendwie eleganter gehen. Es könnten ja viele Formulare und viele Properties sein. |
Re: operator is und as
width TFormB(Sender) do
begin end; |
Re: operator is und as
Ups,
zu schnell geantwotet :wall: Du erzeugst also FormC aus FormA oder FormB heraus???
Delphi-Quellcode:
Hab ich das nun richtig verstanden ?
procedure FormA. ...
begin if Not Assigned( FormC ) then FormC := TFormC.Create( Self ); try FormC.ShowModal; ... finally FreeAndNil( FormC ); end; end; //oder procedure FormB. ... begin if Not Assigned( FormC ) then FormC := TFormC.Create( Self ); try FormC.ShowModal; ... finally FreeAndNil( FormC ); end; end; Dann ist das was du da machst genau richtig. mfg Rumpi |
Re: operator is und as
Hi,
OK ich glaube jetzt hab ich's. Bedingt aber oben genannte Erzeugung!
Delphi-Quellcode:
mfg Rumpi// musst du in eine ander unit stellen ! TXForm = class( TForm ) procedure BLABLA; virtual; abstract; // end; TFormA = class( TXForm ) procedure BLABLA; override; end; TFormB = class( TXForm ) procedure BLABLA; override; end; ... FormC TXForm(Owner).BLABLA; // ruft automatisch TFormA.BLABLA. oder TFormB.BLABLA; |
Re: operator is und as
Delphi-Quellcode:
ja genau so rufe ich FormC auf, den Gedanken mit dem gemeinsamen Parentformular hatt ich auch schon,
procedure FormA. ...
begin if Not Assigned( FormC ) then FormC := TFormC.Create( Self ); try FormC.ShowModal; ... finally FreeAndNil( FormC ); end; end; //oder procedure FormB. ... begin if Not Assigned( FormC ) then FormC := TFormC.Create( Self ); try FormC.ShowModal; ... finally FreeAndNil( FormC ); end; end; schien mir aber ein bisserl zu heftig als Lösung. Trotzdem danke Rumpi für die schnelle Hilfe [edit=Daniel B]Delphi-Tags korrigiert. Mfg, Daniel B[/edit] |
Re: operator is und as
Variation zum Thema:
Delphi-Quellcode:
Code nicht getestet/so nicht funktional/nur sinngemäßer Richtungshinweis :-)
type
TFormA = class; TFormB = class; TFormC = class; TFormA = class(TForm) private DasCForm : TFormC; ... procedure MachBlaBlaMitMir(Sender:TObject); ... end; TFormB = class(TForm) ... procedure MachBlaBlaMitMir(Sender:TObject); ... end; TFormC = class(TForm) private FTuDochWas : TNotifyEvent; ... public property TuDochWas : TNotifyEvent read FTuDochWas write FTuDochWas; ... end; iplementation procedure TFormA.ErzeugeFormC begin if not Assigned(TFormC) then begin DasCForm := TFormC.Create(self); DasCForm.TuDochWas := MachBlaBlaMitMir; DasCForm.Show; end; end; procedure TFormA.MachBlaBlaMitMir(Sender:TObject); begin ... end; procedure TFormB.MachBlaBlaMitMir(Sender:TObject); begin ... end; TFormC.MachAktionMitMutterForm begin if Assigned(FTuDochWas) then begin FTuDochWas; // funzt mit jeder art von Mutterform, egal ob TFormA,-B,D,EFGHIJKLM... // wenn das nur einmal passieren soll, dann: FTuDochWas := nil; end; end; Gruß |
Re: operator is und as
Zitat:
Delphi-Quellcode:
und beim Zugriff ginge das dann so:
type
IBlaBla = interface ['{D277B854-3DFF-4838-B8B9-A87EF5C55B1F}'] procedure SetBla(Value: Integer); function GetBla : Integer; property Blabla : Integer read GetBla write SetBla; end; TFormA = class(Tform,IblaBla) public function GetBla: Integer; procedure SetBla(Value: Integer); end; TFormB = class(TForm,IblaBla) public function GetBla: Integer; procedure SetBla(Value: Integer); end;
Delphi-Quellcode:
var
aForm : TForM; ... procedure TForm1.Button2Click(Sender: TObject); begin if Supports(aForm,IBlaBla) then // vorher prüfen um Exception zu vermeiden (aForm as IblaBla).Blabla := 12; end; janz ohne if else (BTW - geht so (ohne weiteren Code) erst ab Delphi 6 , bei D5 etc müsste noch IUnknown implementiert werden) Bernd |
Re: operator is und as
Hi Bernd,
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:14 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz