Recent

Author Topic: How to set a ScrollBar to Scroll Through a Form vertically  (Read 3780 times)

AfootDesert41

  • New Member
  • *
  • Posts: 15
How to set a ScrollBar to Scroll Through a Form vertically
« on: January 01, 2019, 09:53:56 am »
Hi, Im trying to set a ScrollBar to be able to scroll through a form but haven't managed to do it right yet. This is an image of the form I where I want to implement the ScrollBar:

https://pasteboard.co/HUp964i.png

jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: How to set a ScrollBar to Scroll Through a Form vertically
« Reply #1 on: January 01, 2019, 06:04:26 pm »
I am not 100% sure of what you are asking but I will try..

If you need to set the scrollbar position from code and not from user you can do this..

Form_Name.VertScrollBar.Pos := ??

 
  Keep in mind there is a limit of how far you can scroll a window, due to operating system limits put in place. If
you have a long list to show it maybe better to show it on a control surface where you control the starting line
number and the number of lines to show and do the actual display drawing.


The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5436
  • My goal: build my own game engine using Lazarus
Re: How to set a ScrollBar to Scroll Through a Form vertically
« Reply #2 on: January 02, 2019, 09:09:36 am »
@AfootDesert41

Maybe you can use a TScrollBox. Below is how I will do it:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. { TForm1 }
  27.  
  28. procedure TForm1.Button1Click(Sender: TObject);
  29. var
  30.   frmReport    : TForm;
  31.   scbContainer : TScrollBox;
  32. begin
  33.   frmReport := TForm.Create(Self);
  34.   with frmReport do
  35.   begin
  36.     Caption  := 'Report';
  37.     Width    := 320;
  38.     Height   := 200;
  39.     Left     := Self.Left + 100;
  40.     Top      := Self.Top  + 50;
  41.   end;
  42.   scbContainer := TScrollBox.Create(frmReport);
  43.   with scbContainer do
  44.   begin
  45.     Parent := frmReport;
  46.     Align  := alClient;
  47.     Color  := clWhite;
  48.   end;
  49.   with TLabel.Create(scbContainer) do
  50.   begin
  51.     Parent    := scbContainer;
  52.     Font.Name := 'Ubuntu Mono';
  53.     Caption   :=
  54.       LineEnding +
  55.       '     === SALES REPORT ===' + LineEnding +
  56.       LineEnding +
  57.       ' No      Description     Price'  + LineEnding +
  58.       '  1      Item1             120'  + LineEnding +
  59.       '  2      Item2              62'  + LineEnding +
  60.       '  3      Item3              85'  + LineEnding +
  61.       '  4      Item4              40'  + LineEnding +
  62.       '  5      Item5             115'  + LineEnding +
  63.       '  6      Item6              15'  + LineEnding +
  64.       '  7      Item7              30'  + LineEnding +
  65.       '  8      Item8             205'  + LineEnding +
  66.       '  9      Item9              77'  + LineEnding +
  67.       ' 10      Item10             64'  + LineEnding +
  68.       ' 11      Item11             90'  + LineEnding +
  69.       ' 12      Item12             40';
  70.   end;
  71.   frmReport.ShowModal;
  72.   frmReport.Free;
  73. end;
  74.  
  75. end.

On Ubuntu it works good, but you may need to change the font on line #52 above if the font doesn't show correctly.

wp

  • Hero Member
  • *****
  • Posts: 12864
Re: How to set a ScrollBar to Scroll Through a Form vertically
« Reply #3 on: January 02, 2019, 09:44:44 am »
There is a TForm property AutoScroll which must be set to true in order to show scrollbars when controls are positioned out of the form bounds.

Test:
  • Create a new form
  • Set its AutoScroll to true
  • Increase the form height to 400
  • Add a TButton at Top = 350
  • Reduce the height of the form so that all, or at least part, of the button is no longer visible
  • Notice the vertical scrollbar appearing.

 

TinyPortal © 2005-2018