Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing dynamic forms 1

Status
Not open for further replies.

ColinTod

Programmer
Oct 16, 2006
19
NZ
Hi,
I have a multiple Dynamic Form2's attached to Form1 Pagecontrol tabsheets.
I want to do things on the Form2s based on a button click from Form1. Any sugestions?
==============================
Form1 has:
Button1
Button2
PageControl1

procedure TForm1.Button1Click(Sender: TObject);
var ts: TTabSheet;
begin
ts := TTabSheet.Create(Self);
With ts Do
Begin
Caption := 'Test';
PageControl := PageControl1;
With TForm2.Create( Self ) Do
Begin
Parent := ts;
Visible := True;
Align := alClient;
Windows.SetParent( handle, ts.handle );
End;
End;
Pagecontrol1.ActivePage := ts;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
PageControl1.OwnerDraw:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
var i : integer;
begin
for i := 0 to pagecontrol1.PageCount - 1 do
begin
pagecontrol1.ActivePageIndex := i;
Form2.lOutput.Caption := IntToStr(PageControl1.ActivePageIndex);
// The above line doesn't error but doesn't do anything
// Do something on Form2
// e.g. run the TForm2.Button1Click
// for each sheet
end;
end;

====================================
Form2 has:
lOutput: TLabel;
Button1: TButton;

procedure TForm2.Button1Click(Sender: TObject);
begin
lOutput.Caption := IntToStr(Form1.PageControl1.ActivePageIndex);
end;

===================================
 
Oops, Sorry, Got that wrong.
The line:
Form2.lOutput.Caption := IntToStr(PageControl1.ActivePageIndex);
Does give an error (Access violation at address....) because:
// Application.CreateForm(TForm2, Form2);
is hashed out
 
Adding a class procedure to Form2 will help here.

In Form2 add to the Public declarations

Code:
   public
      [b]class[/b] Procedure DoSomething(Index: Integer);

Declaring the procedure with Class, makes the procedure available even if the form does not exist from other units (if it is added to the other form's uses clause).

Then add the class procedure to Form2.

Code:
[b]class[/b] Procedure TForm2.DoSomething(Index: Integer);
var
   F: TForm2;
   i: Integer;
begin
   F := nil;
   for i := Screen.FormCount - 1 DownTo 0 do
    if (Screen.Forms[i].Name = 'Form2') then
       F := Screen.Forms[I] As TForm2;
   if F <> nil then
      F.lOutput.Caption := IntToStr(Index);
end;

The procedure first checks to see if the form exists, and if so, you now have a reference to it in F. Which now allows you to address the components on the form.

from Form1, you need to add Form2 to the uses clause and simply call the procedure

Code:
var
   FM2:  TForm2;
begin
   FM2.DoSomething(PageControl1.ActivePageIndex);
   ...
end;
 
OK,
I obviously need to start learning abot Classes.
That seems to work except for on minor problem:
The Procedure DoSomething only avare does something on the first tab (0) regardless of the fact that it is run from:

procedure TForm1.Button2Click(Sender: TObject);
var i : integer;
FM2: TForm2;
begin
for i := 0 to pagecontrol1.PageCount - 1 do
begin
pagecontrol1.ActivePageIndex := i;
FM2.DoSomething(PageControl1.ActivePageIndex);
end;
end;

Which is sopposed to cycle through all the tabs.

Any ideas?
 
It looks like to me, that you want to set the caption of a label on form2 (lOutput) to the same value of the activepageindex on your PageControl1 on Form1.

Your code iterates through all of the tabs on the pagecontrol, calling DoSomething with each tab set as the activepagecontrol.

Seems to me that you don't want to iterate through all of the tabs, but on clicking Button2, simply call DoSomething passing the activepageindex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top