How can I access the controls of a form embedded in a page control?
In Form1 I have PageControl. At run time my program c开发者_如何学编程reates tab sheets. In each TabSheet I create Form2. In Form2 I have a Memo1 component. How can I add text to Memo1?
You could do something like this:
(PageControl1.Pages[0].Controls[0] as TForm2).Memo1.Lines.Add('text');
If I get right what are you doing,
procedure TForm1.Button1Click(Sender: TObject);
var
View: TForm;
Memo1, Memo2: TMemo;
Page: TTabSheet;
I: Integer;
begin
View:= TForm2.Create(Form1);
View.Parent:= PageControl1.Pages[0];
View.Visible:= True;
View:= TForm2.Create(Form1);
View.Parent:= PageControl1.Pages[1];
View.Visible:= True;
// find the first memo:
Page:= PageControl1.Pages[0];
Memo1:= nil;
for I:= 0 to Page.ControlCount - 1 do begin
if Page.Controls[I] is TForm2 then begin
Memo1:= TForm2(Page.Controls[I]).Memo1;
Break;
end;
end;
Page:= PageControl1.Pages[1];
// find the second memo:
Memo2:= nil;
for I:= 0 to Page.ControlCount - 1 do begin
if Page.Controls[I] is TForm2 then begin
Memo2:= TForm2(Page.Controls[I]).Memo1;
Break;
end;
end;
if Assigned(Memo1) then Memo1.Lines.Add('First Memo');
if Assigned(Memo2) then Memo2.Lines.Add('Second Memo');
end;
I see one big problem with this code--Memo2 is going to have exactly the same value as Memo1 as there's no difference in the search loops. Also, if this code is complete then there's nothing but the form on the page, there's no reason for a search loop at all.
VilleK's answer should compile and run, I don't see what you are asking for.
So, I solved my problem with your help. This is my code:
var
ID, I: integer;
Tekstas: string;
View: TForm2;
Memo: TMemo;
Page: TTabSheet;
begin
...
Page := PageControl.Pages[ID];
for i := 0 to Page.ControlCount - 1 do
begin
(PageControl.Pages[ID].Controls[0] as TKomp_Forma).Memo.Lines.Add('['+TimeToStr(Time)+']'+Duom[ID].Vardas+': '+Tekstas);
end;
end;
Hope this helps someone else
精彩评论