views:

137

answers:

4

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?

+3  A: 

You could do something like this:

(PageControl1.Pages[0].Controls[0] as TForm2).Memo1.Lines.Add('text');
Ville Krumlinde
Can you write working example??
gedO
A: 

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;
Serg
Super!! With little modification I manage to do what I want :)Greate job
gedO
Guys help me. When I am use only one TabSheet then everything is going well, but when I use more then gives error "List index out of bounds(1)". Any idea way??
gedO
The list index error is probably because you access the Pages or Controls collection with an invalid index. Loop through Controls like in Sergs example above. Loop through pages like this: for I := 0 to PageControl1.PageCount - 1 do begin (PageControl1.Pages[I].Controls[0] as TForm2).Memo1.Lines.Add('text'); end;
Ville Krumlinde
A: 

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.

Loren Pechtel
I think you meant to post your first paragraph as a comment on Serg's answer, and the second paragraph as a comment to Ville's answer. Neither paragraph is an answer to the question.
Rob Kennedy
A: 

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

gedO