Sign in Join
Wiki copytemporary

TTabControl: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TTabControl

OldNewLine
1+**TTabControl** ![](/wiki-images/6/61/Ttabcontrol.png) is a descendant of [[TWinControl]] and can be found on the [[Common Controls tab]] of the [[Component Palette]].
2+
3+It is a tabbed container component which looks identical to a [[TPageControl]]. However, there is a fundamental difference because the control always shows the same page whichever tab is selected. In fact, it containts only a single page. The idea behind this concept is illustrated best by an editor or viewer for text files: When the `TabControl` contains a `TMemo` then the individual tabs refer to the files loaded into the memo; whenever the active tab changes another file is loaded into the (same) memo.
4+
5+Since the `TTabControl` contains only a single page there is no `Pages` property as for the `TPageControl`. The individual tab captions are stored in a `TStrings` property named `Tabs`.
6+
7+The currently selected tab can be identified by means of its index, `TabIndex`.
8+
9+## Navigating between tabs
10+
11+The user can navigate between tabs by clicking on the tab to be used. By code, the same can be achieved by assigning the index of the new tab to the `TabIndex` property of the `TTabControl`.
12+
13+Whenever another tab is displayed two events are fired:
14+
15+- `OnChange` is called **after** the new tab has become active. In the file editor example, the new file contents must be loaded into the memo shown on the `TTabControl`.
16+- `OnChanging` is called **before** the new tab will becomes active. In the file editor example, the contents loaded into the memo must be stored away before it will be replaced by the data for the new tab. Moreover, this event has a parameter `AllowChange` which can be set to `false` to prevent the tab change, for example because the data shown for the previously selected tab have been edited and contain some error to be fixed.
17+
18+## Adding tabs
19+
20+- At designtime, right-click on the `TTabControl` and select the option *Add page* from the context menu.
21+- At runtime, add a new string to the `Tabs` property:
22+
23+``` Pascal
24+procedure TForm1.AddTabBtnClick(Sender: TObject);
25+begin
26+ TabControl1.Tabs.Add('abc');
27+end;
28+```
29+
30+## Deleting tabs
31+
32+- At designtime, right-click on the `TTabControl` and select the option *Delete tab* from the context menu.
33+- At runtime, delete the item with the specific index from the `Tabs` list.
34+
35+``` Pascal
36+procedure TForm1.DeleteTabBtn(Sender: TObject);
37+begin
38+ if TabControl1.TabIndex > -1 then
39+ TabControl1.Tabs.Delete(TabControl1.tabIndex);
40+end;
41+```
42+
43+## Similar controls
44+
45+- [[TPageControl]]
46+- [[TNotebook]]
47+
48+## See also
49+
50+- [[doc:lcl/comctrls/ttabcontrol.html|TTabControl doc]]
51+- [[doc:lcl/extctrls/multi-pagecontrols.html|Multi-page controls doc]]
52+- [[TPageControl]]
53+
54+---
55+*From the Free Pascal wiki, page "TTabControl", as it stood on 2021-02-06 after a change by Wp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history