2
votes

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!

2

2 Answers

3
votes

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

PageControl.ActivePage := NewTab;
3
votes

A PageControl has properties ActivePage and ActivePageIndex, which you can use for this. ActivePage is the most convenient one in this case, since you can just call

PageControl1.ActivePage := NewTab;