Schritt 2 - Implementieren einer Eigenschaft zum Angeben des Formats

Aus RAD Studio
Wechseln zu: Navigation, Suche

Nach oben zu Erstellen einer mit Stilen versehenen FireMonkey-Komponente durch Erweitern einer vorhandenen Komponente


Für die im ersten Schritt erstellte Komponente TClockLabel muss das Datums- und Zeitformat definiert werden, damit – wenn in der Komponente die aktuelle Uhrzeit als Eigenschaft angezeigt wird – Komponentenbenutzer diese angeben können.

Implementieren einer Eigenschaft zum Angeben des Formats

Fügen Sie die folgende Zeile in den published-Abschnitt von TClockLabelclass ein, um die Eigenschaft Format zu implementieren:

In Delphi:

 property Format: String;

In C++:

__property String Format;

Fügen Sie außerdem dem public-Abschnitt eine Definition des Konstruktors wie folgt hinzu:

In Delphi:

 constructor Create(AOwner: TComponent); override;

In C++:

__fastcall TClockLabel(TComponent* Owner);

Nachdem die Eigenschaft Format geändert wurde, muss auch die Beschriftung (Label) aktualisiert werden. Führen Sie zum Aktualisieren der Beschriftung die Methode UpdateLabel als private-Member ein.

Der Code sollte wie folgt aussehen:

In Delphi:

type
TClockLabel = class(TLabel)
private
  { Private declarations }
  procedure UpdateLabel;
protected
  { Protected declarations }
public
  { Public declarations }
  constructor Create(AOwner: TComponent); override;
published
  { Published declarations }
  property Format: String;
end;

In C++:

//header file
class PACKAGE TClockLabel : public TLabel {
private:
	void __fastcall UpdateLabel(void);

protected:
public:
	__fastcall TClockLabel(TComponent* Owner);

__published:
	__property String Format;
};

Drücken Sie nach Einfügen dieser Zeile STRG+UMSCHALT+C. Die IDE fügt automatisch eine Methode zum Setzen der Eigenschaft Format und eine Struktur (Skeleton) für den Konstruktor hinzu. Der Code sollte nun wie folgt lauten:

In Delphi:

type
TClockLabel = class(TLabel)
private
  FFormat: String;
  procedure SetFormat(const Value: String);
public
  constructor Create(AOwner: TComponent); override;
published
  property Format: String read FFormat write SetFormat;
end;


{ TClockLabel }

constructor TClockLabel.Create(AOwner: TComponent);
begin
  inherited;
end;

procedure TClockLabel.UpdateLabel;
begin

end;

procedure TClockLabel.SetFormat(const Value: String);
begin
  FFormat := Value;
end;

In C++:

//header file
class PACKAGE TClockLabel : public TLabel {
private:
	String FFormat;

	void __fastcall UpdateLabel(void);
	void __fastcall SetFormat(const String Value);

protected:
public:
	__fastcall TClockLabel(TComponent* Owner) ;

__published:
	__property String Format = {read = FFormat, write = SetFormat};
};


//source file 
__fastcall TClockLabel::TClockLabel(TComponent* Owner) : TLabel(Owner) {
}
void __fastcall TClockLabel::UpdateLabel(void) {
}

void __fastcall TClockLabel::SetFormat(const String Value) {
	FFormat = Value;
}

Gemäß der Dokumentation für System.SysUtils.DateTimeToString ist der typische Wert für das Datum-/Zeitformat 'c'. Beginnen Sie also das Feld FFormat mit 'c'.

In Delphi

constructor TClockLabel.Create(AOwner: TComponent);
begin
  inherited;
  FFormat := 'c';
end;

In C++:

//source file
__fastcall TClockLabel::TClockLabel(TComponent* Owner) : TLabel(Owner) {
	FFormat = 'c';
}

Implementieren Sie nun die Methoden UpdateLabel und SetFormat wie folgt:

In Delphi:

procedure TClockLabel.UpdateLabel;
begin
  Text := FormatDateTime(FFormat, Now);
end;

procedure TClockLabel.SetFormat(const Value: String);
begin
  FFormat := Value;
  UpdateLabel;
end;

In C++:

void __fastcall TClockLabel::UpdateLabel(void) {
	Text = FormatDateTime(FFormat, Now());
}

void __fastcall TClockLabel::SetFormat(const String Value) {
	FFormat = Value;
	UpdateLabel();
}

Erben von Stilen der übergeordneten Komponente

Verwenden Sie folgenden Code, wenn Ihre Komponente Stile der übergeordneten Komponente erben soll:

Delphi:

type
TClockLabel = class(TLabel)
private
  { Private declarations }
  procedure UpdateLabel;
  function GetDefaultStyleLookupName: string; override;
protected
  { Protected declarations }
public
  { Public declarations }
  constructor Create(AOwner: TComponent); override;
published
  { Published declarations }
  property Format: String;
end;
function TMyButton.GetDefaultStyleLookupName: string;
begin
  Result := Self.GetParentClassStyleLookupName; // stylename of the parent component
end;

C++:

//header file
class PACKAGE TClockLabel : public TLabel {
private:
	void __fastcall UpdateLabel(void);
	System::UnicodeString __fastcall GetDefaultStyleLookupName(void);
protected:
public:
	__fastcall TClockLabel(TComponent* Owner);

__published:
	__property String Format;
};
System::UnicodeString __fastcall TClockLabel::GetDefaultStyleLookupName() {
	return this->GetParentClassStyleLookupName(); // stylename of the parent component
}

Zurück

Weiter

Siehe auch