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.
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.
The currently selected tab can be identified by means of its index, TabIndex.
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.
Whenever another tab is displayed two events are fired:
- 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.
- 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.
- At designtime, right-click on the TTabControl and select the option Add page from the context menu.
- At runtime, add a new string to the Tabs property:
procedure TForm1.AddTabBtnClick(Sender: TObject); begin TabControl1.Tabs.Add('abc'); end;
- At designtime, right-click on the TTabControl and select the option Delete tab from the context menu.
- At runtime, delete the item with the specific index from the Tabs list.
procedure TForm1.DeleteTabBtn(Sender: TObject); begin if TabControl1.TabIndex > -1 then TabControl1.Tabs.Delete(TabControl1.tabIndex); end;