Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Property TFont in neuer Kompo klappt nicht (https://www.delphipraxis.net/13659-property-tfont-neuer-kompo-klappt-nicht.html)

Nalincah 23. Dez 2003 13:36


Property TFont in neuer Kompo klappt nicht
 
Wie kann ich in ner neuen Kompo ne Eingenschaft vom Typ TFont hinzufügen. Im grunde das gleiche wie die Eigenschaft "Font" beim Button oder nem Label. Bei mir klappt das nicht.

Immer wenn ich im OI bei meiner Kompo die Font ändern will kommt das:

Zitat:

nil kann nicht zu TFont zugewiesen werden
Mein Sourcecode (Ausschnitte):

Delphi-Quellcode:
type TCSTyp = (tAdresse, tRechnung, tLieferung);
       TCSAdressLabel = class(TCSQRRichText)
       private
         FUeberschriftFont:TFont;
       public
         constructor Create(AOwner:TComponent);override;
         destructor Destroy;override;
       published
         property UeberschriftFont : TFont read FUeberschriftFont Write FUeberschriftFont;
       end;

choose 23. Dez 2003 13:44

Re: Property TFont in neuer Kompo klappt nicht
 
Wenn Du das direkte Beschreiben einer Exemplarvariablen zulässt, ist die Referenz auf das zuvor erzeugte Exemplar verloren. Darüber hinaus ist nicht klar, wo das zugewisene Exemplar letztlich wann freigegeben wird:
Delphi-Quellcode:
myFont:= TFont.Create;
try
  AnObject.Font:= myFont;
  AnotherObject.Font:= myFont;
finally
  FreeAndNil(myFont);
end;
Verwende stattdessen eine transparente Zuweisung der Form
Delphi-Quellcode:
TMyClass = class
private
  FFont: TFont;
  procedure SetFont(const AValue: TFont);
public
  property Font: TFont
    read FFont
    write SetFont;
//...

procedure TMyClass.SetFont(const AValue: TFont);
begin
  FFont.Assign(AValue);
end;
wobei Du das Exemplar hinter FFont einmalig im Konstruktor erzeugst, bzw im Destruktor zerstörst.

Nalincah 23. Dez 2003 13:53

Re: Property TFont in neuer Kompo klappt nicht
 
Zitat:

Zitat von choose
wobei Du das Exemplar hinter FFont einmalig im Konstruktor erzeugst, bzw im Destruktor zerstörst.

Was meinst du damit?

choose 23. Dez 2003 14:07

Re: Property TFont in neuer Kompo klappt nicht
 
Irgendwo muss FFont mit einem Objekt "belegt" werden. Das sollte, wie bei Objekten, die aus anderen Objekten zusammengesetzt (aggregiert) sind, innerhalb des Konstruktors geschehe. zB so:
Delphi-Quellcode:
constructor TMyClass.Create;
begin
  inherited;
  FFont:= TFont.Create;
  //.. some more initialization code
end;
Das Exemplar sollte dann spätestens im Destruktor wieder freigegeben werden
Delphi-Quellcode:
destructor TMyClass.Destroy;
begin
  FreeAndNil(FFont);
  inherited;
end;

Nalincah 23. Dez 2003 14:17

Re: Property TFont in neuer Kompo klappt nicht
 
AHHH. Vielen Dank. Ich wollt erst das AValue anstatt FFont im Constructor erstellen :wall: Klappte natürlich nicht


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:12 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz