I have created a component in Fire-monkey and also created a TEdit inside it. My component has a String Property named Value that by pouting any string to it , My Component will show that in Tedit. at design Time every thing is OK. but at Run time no thing shows in Tedit My Code is
type
TMyComponent = class(TPanel)
private
{ Private declarations }
Edit:TEdit;
FValue:String;
Procedure SetValue(Const Value:String);
protected
{ Protected declarations }
Constructor Create(Aoner:TComponent); Override;
Destructor Destroy; Override;
public
{ Public declarations }
published
{ Published declarations }
Property Value:String Read FValue Write SetValue;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyComponent]);
end;
Constructor TMyComponent.Create(Aoner:TComponent);
begin
Inherited;
Width:=100;
Height:=100;
Edit:=TEdit.Create(Self);
Edit.Parent:=Self;
Edit.Width:=30;
Edit.Text:='';
Edit.Align:=TAlignLayout.Scale;
end;
Procedure TMyComponent.SetValue(const Value: string);
begin
FValue:=Value;
Edit.Text:=FValue;
end;
Destructor TMyComponent.Destroy;
begin
Edit.Destroy;
Inherited Destroy;
end;
end.
What Should I do?
Initially I thought you were having problems both with creating and using your component at runtime. Instantiating and using your component at runtime works perfectly ok.
After the comment by EugeneK I now understand that the component doesn't work, if you instantiate it at design time and attempt to change the text property of the
TEdit
at runtime.The reason can be traced to the fact that your component is a composed type and constructed from subcomponents (well, only a
TEdit
). If theStored
property of the subcomponent(s) are not set toFalse
they might get streamed several times at design time, f.ex. when you switch between form view and text view of the form.See documentation and scroll down to TCalender: Constructed Complexity where you can find the following:
The result after switching the form view to text view twice:
while this is how it should look like:
The redundant
TEdit
controls will cover the one you create in the constructor.The correction to your code: Add the marked line