Solved

Creating new TTabSheet dynamically

Posted on 2006-07-07
10
456 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
Comment Utility
// 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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:klompen
Comment Utility
Why is it not deallocated after the procedure finished ?

And how about creating the contents of the tab ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 10

Accepted Solution

by:
atul_parmar earned 300 total points
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now