Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating new TTabSheet dynamically

Posted on 2006-07-07
10
Medium Priority
?
467 Views
Last Modified: 2010-04-05
Hi,

I have a TPageControl in the main form. How do I add a new page (TTabSheet) and its contents dynamically ?

For example, when a user click a button, a new page will be added. The new page will have a TPanel and inside the TPanel will have a TMemo.

Also, the new page can be deleted including all its objects, for example by using a delete button.

Please if someone can show me some codes how to do this.

Thanks.
0
Comment
Question by:klompen
[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
  • 3
  • 3
  • 2
10 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17057927
// Add Tab
var
  MyTab : TTabSheet;
begin
  MyTab := TTabSheet.Create(PageControl1);
  PageControl1.InsertControl(MyTab);
  MyTab.PageControl := PageControl1;
  MyTab.Caption := 'MyTab';
  MyTab.TabVisible := True;
end;

// delete tab
var
  MyTab : TTabSheet;
begin
  MyTab := PageControl1.ActivePage;
  MyTab.PageControl := nil;
  MyTab.Free;
end;
0
 

Author Comment

by:klompen
ID: 17058998
I am confuse with "local variable" and "global variable".

If I create the button onclick like this :

procedure TMyForm.Button1Click ....
// Add Tab
var
  MyTab : TTabSheet; // LOCAL ???
begin
  MyTab := TTabSheet.Create(PageControl1);
  PageControl1.InsertControl(MyTab);
  MyTab.PageControl := PageControl1;
  MyTab.Caption := 'MyTab';
  MyTab.TabVisible := True;
end;

Does it "MyTab" only available in the scope of procedure TMyForm.Button1Click ... ???

I want MyTab and its contents available troughout the entire applications.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17059140
And one correction to the above code, It does not require
PageControl1.InsertControl(MyTab);
>>I am confuse with "local variable" and "global variable".

The variable is local but the tab will remain there because it is created and associated while button click. The memory allocated for the tabsheet is not deallocated so it will remain there
e.g.
MyTab := TTabSheet.Create(PageControl1);
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.

 

Author Comment

by:klompen
ID: 17060224
Why is it not deallocated after the procedure finished ?

And how about creating the contents of the tab ?
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 1200 total points
ID: 17070978
>>Why is it not deallocated after the procedure finished ?
if we deallocate then it will be destroyed so you could not see and play with it. But no need to worry it will not have memory leak. the pagecontrol will destroy it automatically if you don't delete it with the delete button.

>> And how about creating the contents of the tab ?

procedure TMyForm.Button1Click ....
// Add Tab
var
  MyTab : TTabSheet; // LOCAL ???
  MyPanel : TPanel;
  MyMemo : TMemo;
begin
  MyTab := TTabSheet.Create(PageControl1);
  MyTab.PageControl := PageControl1;
  MyTab.Caption := 'MyTab';
  MyTab.TabVisible := True;
  // create content
  MyPanel := TPanel.Create(MyTab);
  MyPanel.Parent := MyTab;
  MyPanel.Visible := True;
  //MyPanel.Left, width, height, etc.
  MyMemo := TMemo.Create(MyPanel);
  MyMemo.Parent := MyPanel;
  MyMemo.Visible := True;
  // MyMemo.left, width, height, etc
end;

See that the appropriate owner is passed when we create the contents so that it will can be destroyed properly. If you are confused with Parent/Owner see the Delphi help.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17078609
MyTab is just a pointer, so it is ok if it drops out of scope in the procedure, the object stays alive. however it's owner will free it when it is freed itself
0
 

Author Comment

by:klompen
ID: 17120660
So, if I delete a tab, all the contents in that tab will be free-ed (deleted as well) automatically right ?
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18261303
Yes, atul_parmar's answer is correct.
1 final note in response to the author's last question (don't accept this, just atul_parmar's)
If the components on the Tab have the tab as the owner, (which they are in the example above "MyPanel := TPanel.Create(MyTab);" )
then yes, they will be free-ed when the tab is free-ed
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

730 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