Search code examples
delphifiremonkeytabcontroltabitem

How to insert another control into TTabItem in Firemonkey


I would like to insert an additional TText into the "tabbed" part TTabItem of a TTabControl, so that I can have another text of a different color.

It seems a control can't be moved to that "tabbed" portion.

Is there a way to achieve this?

enter image description here


Solution

  • As the requirement is to have a simple text displayed on a TTabItem this can be easily implemented using the onPaint event of the TTabItem control. So this code:

    procedure TForm1.TabItem1Paint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
    var
      R: TRectF;
    begin
      // Use only 1/3 top part of the canvas
      R:=RectF(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom / 3);
      // Set font color and size
      Canvas.Fill.Color := TAlphaColors.Seagreen;
      Canvas.Font.Size := 12;
      // Draw text
      Canvas.FillText(R, 'New!', False, 1, [], TTextAlign.Center, TTextAlign.Center);
    end;
    

    Produces this:

    TabItem with custom text drawn on top