Solved

Creating new TTabSheet dynamically

Posted on 2006-07-07
10
465 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
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!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Performance of SQL statement 37 147
Broadcast a message using ICS 2 51
Get monday of current week where a week always starts on monday 2 37
QuickReport, Delphi, Shape 12 48
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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