Step 2 - Implement a Property to Specify the Format

From RAD Studio
Jump to: navigation, search

Go Up to Creating a Styled FireMonkey Component by Extending an Existing Component


The TClockLabel component created in Step 1 needs to define the date time format when it shows the current time as a property so that component users can specify it.

Implementing a Property to Specify the Format

To implement the Format property, add the following one line to the published section of TClockLabelclass:

In Delphi:

 property Format: String;

In C++:

__property String Format;

Also, add a definition of the constructor to the public section as follows:

In Delphi:

 constructor Create(AOwner: TComponent); override;

In C++:

__fastcall TClockLabel(TComponent* Owner);

After Format is changed, Label needs to be updated. To take care of updating the label, introduce the UpdateLabel method as a private member.

The code should look like this:

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;
};

After you insert this line, press CTRL+SHIFT+C. The IDE automatically adds a setter for the Format property, and a skeleton for the constructor. The code now should look like this:

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;
}

According to the documentation for System.SysUtils.DateTimeToString, the typical value for date time format is 'c'. So, initialize the FFormat field with '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';
}

Now implement the UpdateLabel and SetFormat methods as follows:

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();
}

Inheriting Styles of the Parent Component

If you want your component to inherit styles from the parent component, use the following code:

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
}

Previous

Next

See Also