Search code examples
delphilazarus

How to make a new (dynamic) tab be the active page


I tagged both Lazarus and Delphi as both seem to be similar (100%) for everything I've done so far. But my dev platform is Lazarus.

I'm dynamically creating a tab sheet like this:

procedure TForm1.cmdTabButtonClick(Sender: TObject);
var
  NewTab: TTabSheet;
begin
  NewTab := TTabSheet.Create(PageControl1);
  NewTab.PageControl:= PageControl1;
  NewTab.Caption:='hi';
//  NewTab.TabVisible:=true;
//  newtab.SetFocus;
end;

The last two lines in the procedure are commented. Without them the code works but the new tab is not the one on top. I have to click it for it to come on top.

But if I uncomment those two lines, the program crashes stating that an invisible object cannot have focus.

any advice on how this can be fixed?

Many thanks!


Solution

  • You need to set the ActivePage property of the page control. Like this:

    PageControl.ActivePage := NewTab;