PDA

View Full Version : Ultimate form tutorial



jhildy
09-11-2007, 02:15 AM
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

Basic form setup
Form editor

Done.

Section 2: Buttons

Adding a button
Changing button text
Button events


Section 3: Labels

Adding a label
Changing label


Section 4: Edit Boxes

Adding an edit box
Getting text from an edit box
Password character
Edit box events


Section 5: Combo boxes

Adding a combo box
Adding combo box items
Getting combo box text
Combo box events


Section 6: Check boxes/Radio buttons

Adding check boxes/radio buttons
Check box/radio button checked?
Events


Section 7: Changing colors

Changing background color
Changing text color


Section 8: Misc form objects

Bevels
Group boxes
Images
Progress bar

Section 9: Multiplayers

Already made a tut on this


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
http://img521.imageshack.us/img521/6172/02cx5.png (http://imageshack.us)

This is what you see when you click on the form editor.
http://img204.imageshack.us/img204/4966/02py5.png (http://imageshack.us)
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
09-11-2007, 02:23 AM
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
09-11-2007, 02:25 AM
Cool! GJ jhildy!

HyperSecret
09-11-2007, 02:52 AM
i may add forms once you get this tut done :D

BobboHobbo
09-11-2007, 05:12 AM
Very nice for just one section, cant wait to see it when its all done.

GJ

MasterKill
09-11-2007, 06:00 AM
Gs :) keep it up :D

(good start ;))

Timothegreat
01-25-2008, 12:50 AM
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
01-25-2008, 12:52 AM
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
01-29-2008, 10:15 AM
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
02-21-2008, 05:07 PM
can you please finish this tut?