Jest sobie komponent TMojKomponent (kod okrojony do minimum) na bazie panelu, na którym leżą komponenty FImage (wycięty) i FLabel. Po upuszczeniu komponentu na formatkę Designer-a, Caption komponentu FLabel ustawia się na 'Caption'. Tu OK.
Gdy w komponencie TMojKomponent ustawię property Caption na inny niepusty łańcuch, to we FLabel ustawi się taki Caption, zarówno w Designer-ze, jak i po uruchomieniu programu. Tu też OK.
Jeśli natomiast ustawię property Caption mojego komponentu na pusty łańcuch (może zaistnieć taka potrzeba), to w Designer-ze zostanie on ustawiony w FLabel, ale po uruchomieniu programu już nie (nie wykona się metoda SetCaption mojego komponentu), przez co w FLabel będzie wyświetlane 'Caption'.
Zauważyłem, że jeśli w mojej kontrolce (jak również np. w TPanel, TButton) jest ustawiony Caption jako pusty łańcuch, to w DFM-ie nie występuje pole Caption w ustawieniach kontrolki, co zapewne powoduje, że w runtime nie jest wywoływany setter property Caption komponentu.
Czy da się zrobić, pomijając jakieś kombinacje z ustawianiem spacji jako Caption, żeby pusty Caption był wyświetlany także po uruchomieniu?
unit MojKomponent;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, ExtCtrls, Forms;
type
TMojKomponent = class(TPanel)
private
FLabel: TLabel;
procedure SetCaption(const aCaption: String);
function GetCaption: String;
protected
procedure CreateWnd; override;
public
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
property Caption: string read GetCaption write SetCaption;
end;
procedure register;
implementation
procedure register;
begin
RegisterComponents('MOJE', [TMojKomponent]);
end;
constructor TMojKomponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ParentBackground := false;
ParentCtl3D := False;
BevelOuter := bvNone;
BorderStyle := bsSingle;
BorderWidth := 1;
FLabel := TLabel.Create(Self);
FLabel.Parent := self;
FLabel.ParentColor := false;
FLabel.Transparent := true;
FLabel.Left := 5; //pozycja Labela na panelu
FLabel.Top := 5;
FButtonLabel.Caption := 'Caption';
end;
procedure TMojKomponent.CreateWnd;
begin
inherited CreateWnd;
inherited Caption := ''; //czyszczenie Captiona panelu
end;
procedure TMojKomponent.SetCaption(const aCaption: string);
begin
if FLabel.Caption <> aCaption then
FLabel.Caption := aCaption;
end;
function TMojKomponent.GetCaption: string;
begin
Result := FLabel.Caption;
end;
end.