Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating new TTabSheet dynamically

Posted on 2006-07-07
10
Medium Priority
?
468 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
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

886 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