SRL Archive - OSR Advanced Scripting Tutorials

Ultimate form tutorial


jhildy

2007-09-10 22:15:11

Hey this is my complete form tutorial. I am going to compile all my knowledge about forms together to make one massive tutorial. I will break it down into sections. Since i have school i will not release this all at once but when i get the time.

Section 1: Basics
[LEFT]Basic form setup
Form editor
[/LEFT]

Done.

Section 2: Buttons
[LEFT]Adding a button
Changing button text
Button events
[/LEFT]


Section 3: Labels
[LEFT]Adding a label
Changing label
[/LEFT]


Section 4: Edit Boxes
[LEFT]Adding an edit box
Getting text from an edit box
Password character
Edit box events[/LEFT]


Section 5: Combo boxes
[LEFT]Adding a combo box
Adding combo box items
Getting combo box text
Combo box events[/LEFT]


Section 6: Check boxes/Radio buttons
[LEFT]Adding check boxes/radio buttons
Check box/radio button checked?
Events[/LEFT]


Section 7: Changing colors
[LEFT]Changing background color
Changing text color
[/LEFT]


Section 8: Misc form objects
[LEFT]Bevels
Group boxes
Images
Progress bar[/LEFT]


Section 9: Multiplayers
[LEFT]Already made a tut on this[/LEFT]


Basics
Basic form setup
The basic way to set up a form is described in the scar manual. I will post here and explain this further though.
program New;
var
frmDesign : TForm;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'frmDesign';
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.
This is the base of every form created through the form editor. The first thing done is the forms variables are created.
program New;
var
frmDesign : TForm;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := True;
frmDesign.PixelsPerInch := 96;
end;
If you used a form editor to get the form it will basically give you this code.
This is just the variable frmDesign declared as a Tform. Then it is created through the procedure initform. What you made the form look like in the form editor will all be labeled here.
One thing you must remember though is to change
frmDesign.Visible := True;
To
frmDesign.Visible := False;
If you do not you will get an error.
The next step is to make the form thread safe and make sure the script shows it. That is where these procedures come in.
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;
The reason is has to be thread safe is because there is a good chance scar will crash. Most of scars functions already are thread safe but not all of them are.
The next step is to just call the thread safe procedures in the main loop.
begin
SafeInitForm;
SafeShowFormModal;
end.
After that you will have a script that looks like this.
program New;
var
frmDesign : TForm;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'frmDesign';
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.
They press play and you will see a blank form.

Form editor
The form editor is an easy to use built in feature of scar. It makes form making extremely easy when it comes to designing the form. I will explain the basic components of the form editor in this section.
You can open the form editor by going to Tools->Form editor


This is what you see when you click on the form editor.

The form template is where you drag your items to. The objects are what you are putting on the form. The object inspector is how you change the properties of objects. I will go into more detail with each tutorial that comes up.

More to come when i get the time!

itSchRis917

2007-09-10 22:23:54

Wow, i imagine this will be a VERY good tutorial when you get around to adding all of those other things.

[offtopic, but still having to do with forms] since you are incredibly good at forms, do you think you could take a look at my thread? http://www.villavu.com/forum/showthread.php?t=17197?p=212100#post212100
[/offtopic, but still having to do with forms]

Harry

2007-09-10 22:25:31

Cool! GJ jhildy!

HyperSecret

2007-09-10 22:52:31

i may add forms once you get this tut done :D

BobboHobbo

2007-09-11 01:12:35

Very nice for just one section, cant wait to see it when its all done.

GJ

MasterKill

2007-09-11 02:00:49

Gs :) keep it up :D

(good start ;))

Timothegreat

2008-01-24 19:50:07

even though this is a gravedig i feel this tutorial is very important (so its not really a gravedig)

and i would love to say Jhildy did a wonderful job :D

P1nky

2008-01-24 19:52:20

wow thanks for the GRAVEDIG lol i did not even know this tut EXISTED thanks though wished bobbo and jhidly finished :( on the tuts but thanks ill read it

Negaal

2008-01-29 05:15:40

Well, I can handle simple forms pretty well, but this doesn't work =/, is it about windows vista or my script or scar 3.14, List is name of students in my class, "on loll", means "is stupid"

program New;

var
frmDesign : TForm;
Label1 : TLabel;
Button1 : TButton;
list : TStringList;
i : integer;

procedure buttonclick(sender: TObject);
begin
Label1.Caption := List[i]+' on loll!';
i := i + 1;
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 250;
frmDesign.Top := 114;
frmDesign.Width := 370;
frmDesign.Height := 186;
frmDesign.Caption := 'frmDesign';
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;
Label1 := TLabel.Create(frmDesign);
Label1.Parent := frmDesign;
Label1.Left := 16;
Label1.Top := 80;
Label1.Width := 3;
Label1.Height := 13;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 16;
Button1.Top := 24;
Button1.Width := 321;
Button1.Height := 41;
Button1.Caption := 'Button1';
Button1.TabOrder := 8;
Button1.OnClick:= @buttonclick;
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
SetLength(List, 7);
List[0] := 'Romet';
List[1] := 'Tõnis';
List[2] := 'Allen';
List[3] := 'Tiit';
List[4] := 'Kati';
List[5] := 'Mihkel';
List[6] := 'Sander';

SafeInitForm;
writeln('1');
SafeShowFormModal;
writeln('2');
end.

Edit: It doesn't writeln anything to debug, and if Visible is wrong, then I'd have to admit I tryed both ^^

P1nky

2008-02-21 12:07:05

can you please finish this tut?