gedO
gedO

Reputation: 546

How can I access the controls of a form embedded in a page control?

In Form1 I have PageControl. At run time my program creates tab sheets. In each TabSheet I create Form2. In Form2 I have a Memo1 component. How can I add text to Memo1?

Upvotes: 2

Views: 2532

Answers (4)

gedO
gedO

Reputation: 546

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

Upvotes: 0

Loren Pechtel
Loren Pechtel

Reputation: 9093

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.

Upvotes: 0

kludg
kludg

Reputation: 27493

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;

Upvotes: 1

Ville Krumlinde
Ville Krumlinde

Reputation: 7131

You could do something like this:

(PageControl1.Pages[0].Controls[0] as TForm2).Memo1.Lines.Add('text');

Upvotes: 3

Related Questions