0

This is about PageControl with dynamic tabs. I have 5 buttons (Button1, Button2, Button3, Button4, Button5).

I want the following:

  1. Each click on a button will open a new TtabSheet.
  2. When clicked on the same button the 'Ttabsheet' already opened, should be shown again.

How to do this?

Nasreddine Galfout
  • 2,550
  • 2
  • 18
  • 36
Agus Budhi
  • 13
  • 2
  • So when you click on a button you want the page to be displayed? – Alberto Miola May 03 '17 at 12:59
  • and can you explain the second part? – Nasreddine Galfout May 03 '17 at 13:01
  • Welcome to Stack Overflow! Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight May 03 '17 at 13:08
  • I have success to add page on every button, but when i click the same button again there is add a new page. i want if i click the same button that show the recent opened Tab page. – Agus Budhi May 03 '17 at 13:20
  • ok so when you press the button a page is added. and when you press the same button you expect it to show the page already added and not add a new page. is that what are you asking for? – Nasreddine Galfout May 03 '17 at 13:29
  • @Nasreddine Abdelillah Galfout : That's right, that's what I mean – Agus Budhi May 03 '17 at 13:32
  • You still need to show us what code you are working on (or a simplified version). It makes it much easier for us to help you. – Dsm May 03 '17 at 13:32

2 Answers2

0

Just define variable like fPreviousTabIndex inside your form class, where you store the last state (ActivePageIndex property of the TPageControl).

The next part is already simple...

procedure TForm1.Button2Click(Sender: TObject);
const
  DESIRED_PAGE_INDEX = 2;
begin
  if PageControl.ActivePageIndex = DESIRED_PAGE_INDEX then
    PageControl.ActivePageIndex := fPreviousTabIndex
  else
  begin
    fPreviousPageIndex := PageControl.ActivePageIndex;
    PageControl.ActivePageIndex := DESIRED_PAGE_INDEX;
  end;
end;

Of course you need to initialize the fPreviousTabIndex during creation of the form.

Jacek Krawczyk
  • 2,083
  • 1
  • 19
  • 25
-1

I don't know why you really want to have five buttons doing the job of creating the tabs and then selecting them, but this is one of the approaches you can try

uses System.Generics.Collections;

...

var
Newtabsheet: Ttabsheet;
Tabs: TList<Ttabsheet>;
Index: array[1..5] of Integer;
Ex: array [1..5] of Boolean;

implementation

Put this on the Formcreate handler

procedure TForm6.FormCreate(Sender: TObject);
begin
Tabs := Tlist<Ttabsheet>.create;
for I=1 to 5 do ex[I]:=false;
end;

And this on each OnClickButton event handler

procedure TForm6.Button1Click(Sender: TObject);
begin
if not(Ex[1])then
  begin
  Newtabsheet := Ttabsheet.Create(PageControl1);
  NewTabSheet.PageControl := PageControl1;
  Newtabsheet.Caption := 'Tab 1';
  Index[1] := Tabs.Count;
  Tabs.Add(Newtabsheet);
  Ex[1] := true;
  end
else
  begin
  Pagecontrol1.ActivePage := Tabs.List[Index[1]];
  end;
end;

procedure TForm6.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Tabs.Free;
end;

Remember changing the numbers.

It is tested on RAD studio Seattle.

Note: based on david comments I have edited my answer. and for further explanation please see this question

Nasreddine Galfout
  • 2,550
  • 2
  • 18
  • 36
  • Yes It can run well, but when I delete the tab it can not re-open the same tab. what should I do? – Agus Budhi May 03 '17 at 14:44
  • when you delete the tab do 'ex(number of the tab) :=false;' and it should do the trick. – Nasreddine Galfout May 03 '17 at 14:46
  • just one thing when you delete the tab you are deleting it from the 'tabs' list right? – Nasreddine Galfout May 03 '17 at 15:08
  • yeah you are right, I just use PageControl1.ActivePage.free with statement list of the tabs active. – Agus Budhi May 03 '17 at 15:54
  • Where should I put the code? Because after I put it on the delete tab that is not working – Agus Budhi May 03 '17 at 17:18
  • 1
    This code is very messy. At the very least it should be using an array rather than 5 variables. Anyway, it's way easier to throw away all of the code and use the TabVisible property of tab sheets. – David Heffernan May 06 '17 at 13:19
  • @NasreddineAbdelillahGalfout I think `TabVisible` property will work as needed as Davids say. – Ilyes Aug 05 '17 at 12:09
  • @DavidHeffernan I improved the formatting, However I don't see how `TabVisible` would make it easier. With the requirement set by the OP _the same button creating the tab should select it when clicked again_ , you would still need to store a reference to the `Ttabsheet`s created dynamically and you will also have to create them and delete them when needed (also required by OP from his first comment ), I really want to see how could I have done this using `TabVisible` – Nasreddine Galfout Sep 09 '17 at 23:51