Solved

Creating new TTabSheet dynamically

Posted on 2006-07-07
10
460 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 300 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 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