Hello,
I am struggling to understand how to do it correctly. The case is the following:
Imagine a button inside a frame or may be another form, but in this particular situation is a frame. Now imagine a label inside it's parent form. When you click button, the caption of the label is modified.
Form uses Frame's unit but not otherwise to avoid circular referencing, so the Frame cannot see the Label1 and something like Form1.Label1.Caption := "text something" is not possible because Label1 is not recognised as a valid identifier.
Our fellow programmer @Handoko proposed a clever but tricky solution (in his case to access an statusbar) in this post:
https://forum.lazarus.freepascal.org/index.php/topic,37456.msg257730.html#msg257730 (https://forum.lazarus.freepascal.org/index.php/topic,37456.msg257730.html#msg257730)
but I haven't been able to replicate the solution in this case.
I found the following references:
http://delphi.xcjc.net/viewthread.php?tid=48799 (http://delphi.xcjc.net/viewthread.php?tid=48799)
and
http://forum.lazarus.freepascal.org/index.php?topic=19459.0 (http://forum.lazarus.freepascal.org/index.php?topic=19459.0)
Why I can't do the following?
ParentForm := GetParentForm(Self); ParentForm.(FindComponent('Label1') as TLabel).Caption := 'you pressed button whatever...';
regards
Thank you taazz,the language operator, as, is used to type cast a type in to an other and the syntax is, SourceObject as TargetType, the brackets around the expression is to instruct the compiler to use the Sourceobject as the memory address for what follows the dot instead of the targetclass/type, in your case the SourceObject is the result of the method ParentForm.FindComponent that can not be splited on the dot, findcomponent does not exists with out parentform, and parentform outside the brackets can be translated as a hard cast from the compiler.
Please, can you explain me the logic behind positioning the brackets the way you do? I don't get it. Or if you know the part or what shall I read in the fpc reference manual that actually treats this basic matter it would be very helpfull. Thanks again.
Imagine a button inside a frame or may be another form, but in this particular situation is a frame. Now imagine a label inside it's parent form. When you click button, the caption of the label is modified.
type
TMyFrameTextEvent = procedure(Sender: TObject; const Text: String) of object;
TFrame1 = class(TFrame)
Button1: TButton;
procedure ButtonClick(Sender: TObject);
public
OnDisplayText: TMyFrameTextEvent;
end;
procedure TFrame1.ButtonClick(Sender: TObject);
begin
if Assigned(OnDisplayText) then
OnDisplayText(Self, 'you pressed button whatever...');
end;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
Frame: TFrame1;
procedure DisplayFrameText(Sender: TObject; const Text: string);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Frame := TFrame1.Create(Self);
Frame.Parent := Self;
...
Frame.OnDisplayText := DisplayFrameText;
end;
procedure TForm1.DisplayFrameText(Sender: TObject; const Text: string);
begin
Label1.Caption := Text;
end;
Why I can't do the following?
ParentForm := GetParentForm(Self); ParentForm.(FindComponent('Label1') as TLabel).Caption := 'you pressed button whatever...';
(ParentForm.FindComponent('Label1') as TLabel).Caption := 'you pressed button whatever...';
var
Temp_Comp: TComponent;
Temp_Label: TLabel;
...
Temp_Comp := ParentForm.FindComponent('Label1');
Temp_Label := Temp_Comp as TLabel;
Temp_Label.Caption := 'you pressed button whatever...';