PDA

View Full Version : Forms TScrollBar tutorial



Freddy1990
11-21-2006, 04:36 PM
In this tutorial we will learn how to add and use the TScrollBar component in SCAR forms.
We begin with a basic script that shows a form:
program ScrollBarTest;

var
frmDesign: TForm;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;

begin
SafeInitForm;
SafeShowFormModal;
end.
http://stuart.existhost.com/freddy/tutimages/menu1.PNG

First to display the scrollbars value we're going to add a label.

So we add the variabledeclaration:
lbl: TLabel;

And we add to the initform procedure:
lbl := TLabel.Create(frmDesign);
lbl.Parent := frmDesign;
lbl.Left := 14;
lbl.Top := 7;
lbl.Width := 95;
lbl.Height := 13;

We don't give the label a text (caption) yet because the scrollbar is going to do that.

This results in the following script:
program ScrollBarTest;

var
frmDesign: TForm;
lbl: TLabel;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Scrollbar Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;

lbl := TLabel.Create(frmDesign);
lbl.Parent := frmDesign;
lbl.Left := 14;
lbl.Top := 7;
lbl.Width := 95;
lbl.Height := 13;
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;

begin
SafeInitForm;
SafeShowFormModal;
FreeForm(frmDesign);
end.

Now, first we add the scrollbar to the variabledeclaration:
sb: TScrollBar;

Next we create the scrollbar in our form by adding the following to the initform procedure:
sb := TScrollBar.Create(frmDesign);

Next we add a line to put the scrollbar on our form:
sb.Parent := frmDesign;

Now we define the scrollbars size and position by putting the following in the initform procedure:
sb.Left := 11;
sb.Top := 47;
sb.Width := 300;
sb.Height := 19;

Next we use the Min and Max propertys which will define the scrollbars starting and ending point, the values i use are also default:
sb.Min := 0;
sb.Max := 100;

We also add the position property to the initform peocedure for setting the startingposition, in this case 0 which is also the default position:
sb.Position

To make the label display the position of the scrollbar, we're going to add the folowing procedure:
procedure OnScroll(sender: TObject);
begin
lbl.Caption := 'Scrollbar position: ' + IntToStr(sb.Position);
end;

Finally we add the procedure we just added to the scrollbar's onscroll event in the initform procedure:
sb.OnChange := @OnScroll;

Now we have our final script:
program ScrollBarTest;

var
frmDesign: TForm;
lbl: TLabel;
sb: TScrollBar;

procedure OnScroll(sender: TObject);
begin
lbl.Caption := 'Scrollbar position: ' + IntToStr(sb.Position);
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Scrollbar Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;

lbl := TLabel.Create(frmDesign);
lbl.Parent := frmDesign;
lbl.Left := 14;
lbl.Top := 7;
lbl.Width := 95;
lbl.Height := 13;

sb := TScrollBar.Create(frmDesign);
sb.Parent := frmDesign;
sb.Left := 11;
sb.Top := 47;
sb.Width := 300;
sb.Height := 19;
sb.Min := 0;
sb.Max := 100;
sb.Position := 0;
sb.OnChange := @OnScroll;
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;

begin
SafeInitForm;
SafeShowFormModal;
FreeForm(frmDesign);
end.
http://stuart.existhost.com/freddy/tutimages/menu6.PNG

Now when we scroll the bar we see the current value show up on the label at the top of the form.

And that concludes this tutorial, have fun.

Pentti
11-21-2006, 05:17 PM
Freddy, you are pretty good at forms! :D Your form tutorials are great! :D

Rick
11-22-2006, 12:02 AM
So many tuts...so much information.