Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Creating predefined panels or tabsheets etc

Posted on 2003-03-10
6
Medium Priority
?
349 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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

572 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