?
Solved

Creating predefined panels or tabsheets etc

Posted on 2003-03-10
6
Medium Priority
?
345 Views
Last Modified: 2010-04-04
Hi,

I am trying to create news tabsheets from and existing tabsheets defined in a "container" form.

While I am able to create one tabsheet I am unable to create any others as it says the name is the same. Also when I exit the program I get an access violation. I guess I am not doing it correctly. Don't understand how to create from an existing set of predefined components.

Code follows

var
i: integer;
nt : TTabSheet;

For i := 1 to 10
begin
nt := Form1.TabSheet1.create(Application); //I guess this should be a type??? Tabsheet from container form
nt.name := 'XYZ' + IntToStr(i);
nt.PageControl := PageControl1; //Pagecontrol to which I wish to add the tabsheet
nt.Caption := 'Page ' + IntToStr(i);
end;

Where am I going adrift?

Cheers

Chris
0
Comment
Question by:ChrisBerry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8104780
use as follows

For i := 1 to 10
begin
  with TTabSheet.create(self) do
  begin
    //name := 'XYZ' + IntToStr(i); forget the name, except u use the name
    PageControl := PageControl1;
    Caption := 'Page ' + IntToStr(i);
    Parent := PageControl1;
  end;
end;

not tested . . .

meikl ;-)

0
 

Author Comment

by:ChrisBerry
ID: 8105258
Hi Meikl,

Thanks for the reply.

That is fine if I just want to create a new tabsheet, however what I want to do is to create new copies of a tabsheet that has other conmponents on it such as buttons, editboxes etc.,

For example if I create a form using the IDE such as
TForm1 = class(TForm)
...
...
...
end;

I can then replicate copies of the form with

f : TForm1;

f := TForm1.create(self);

I wish to do the same with pre-defined tabsheets and panels etc.

Cheers

Chris
0
 
LVL 1

Accepted Solution

by:
merry_prince earned 500 total points
ID: 8110066
Please clone below source codes:
- the components on the template tabsheet need to be owned by the tabsheet,
not the form. The formCreate method takes care of this.
- all control classes used on the tabsheet need to be registered. The
RegisterClasses call in the initialization section takes care of that.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Button1: TButton;
    ListBox1: TListBox;
    ComboBox1: TComboBox;
    CloneButton: TButton;
    Memo1: TMemo;
    procedure CloneButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure ShowStream(ms: TMemoryStream);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.CloneButtonClick(Sender: TObject);
var
  ms: TMemoryStream;
  sheet: TTabSheet;
  S: String;
begin
  ms:= TMemoryStream.Create;
  try
    ms.WriteComponent( Tabsheet1 );
    ms.Position := 0;
    ShowStream(ms);
    sheet := ms.ReadComponent( nil ) as TTabsheet;
    sheet.Pagecontrol := pagecontrol1;
    sheet.Caption := format('Tabsheet%d',[ pagecontrol1.pagecount ]);
  finally
    ms.free
  end; { finally }
end;

procedure TForm1.ShowStream(ms: TMemoryStream);
var
  ts: TStringstream;
begin
  ts:= TStringstream.Create( EmptyStr );
  try
    ObjectBinaryToText( ms, ts );
    ms.Position := 0;
    memo1.text := ts.DataString;
  finally
    ts.free
  end;
end; {ShowStream}

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  c: TControl;
begin
  // make the template tabsheet the owner of all controls on it
  For i:= 0 to Tabsheet1.Controlcount-1 Do Begin
    c:= Tabsheet1.Controls[i];
    Self.RemoveComponent( c );
    Tabsheet1.InsertComponent( c );
  End;
end;

initialization
  RegisterClasses( [TTabSheet, TListBox, TButton, TCombobox] );

end.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:mattlaver
ID: 8110760
I do something similiar, I have a container form and add it multiple times to a page control as:

....

var
  myForm: Tmyform;
  i: integer;
begin
  for i := 0 to 9 do
  begin
    myForm := TmyForm.Create( Application );
    myForm.ManualDock(pagecontrol1, pagecontrol1);
    myForm.Caption := inttostr(i);
    myForm.show;
  end;
end;

I hope this is what you need!

Matt
0
 

Author Comment

by:ChrisBerry
ID: 8116553
Thanks for the replies.

I had come to a similar conclusion that streaming was the answer to my problem but I could not solve the ownership problem.

Cheers

Chris
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8116612
Please note the codes of FormCreate event disposed ownership. i.e. Owned by the tabsheet, ont the form.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question