Adding Scrollable Content

By Erick Engelke

July 25, 2024

Sometimes you may want to have scrollable content such as TPanels with some data, or Calendar Scheduling entries with more data than will fit in the predetermined "window".

I’ve made a sample program which uses TPanels, TScrollPanels, and THTMLLabel to look like this wherever you click on the window:

sample

Of note, the TScrollPanel grows its virtual size to accomodate the HTML we put inside it, and yet the outer dimensions are limited to the TPanel’s size.

Here is the code to accomplish thta.

type
   TMyPanel = class( TPanel )
   public
      scroll : TScrollPanel;
      data : THTMLLabel;
   end;

   TForm1 = class(TForm)
      procedure Form1MouseDown(Sender: TObject; Button: Integer; ShiftKey: Boolean;
                               CtrlKey: Boolean; AltKey: Boolean; X: Integer;
                               Y: Integer);
   private
      { Private declarations }
   public
      { Public declarations }
      mine : TMyPanel;
   end;

implementtion

procedure TForm1.Form1MouseDown(Sender: TObject; Button: Integer;
                                ShiftKey: Boolean; CtrlKey: Boolean;
                                AltKey: Boolean; X: Integer; Y: Integer);
var
  my : TMyPanel;
  sc : TScrollPanel;
  sl : TStringList;
  i : integer;
begin
  my := TMyPanel.Create( self );
  my.Parent := self;
  my.left := x;
  my.top := y;

  // add scroll bars
  sc := TScrollPanel.Create( my );
  sc.parent := my;
  sc.layout.position := lpTopLeft;
  sc.layout.stretch := lsBottomRight;
  sc.ScrollBars := ssBoth;

  my.scroll := sc;

  // create HTML content
  my.data := THTMLLabel.Create( sc );
  my.data.parent := sc;
  my.data.autosize.height := True;
  my.data.autosize.Width := True;
  my.data.Background.Fill.Color := clTransparent;

  // add some text content to the HTMLLbel

  sl := TStringList.Create;
  // so we don't collect carriage returns/linefeeds
  sl.LineSeparator := '';
  for i := 1 to 25 do
    sl.add( 'Line # ' + inttostr(i )+'<br/>');

  my.data.content := sl.Text;
  sl.Free;

end;