SRL Archive - OSR Advanced Scripting Tutorials

Adding Tabs To Forms


SKy Scripter

2007-07-15 18:11:13

How To Add Tabs



In Order To Make Tabs You Must Have A PageControl


var
PageControl : TPageControl;

Secondly You Must Have Your TabPages


var
TabPages : array [1..2] of TTabSheet;

Now That You Know What Is Needed To Make Tabs Lets Start.

Will Start by declaring are Variables…


program AddingTabs;

// Adding variables

const
Amount = 10; // How Many Sheets


var
MainForm : TForm; // Are Main TForm
PageControl : TPageControl; // This is Are page Control Which controls are tabs
TabPages : array [1..Amount] of TTabSheet; // Array of 2 TTabSheet's (Which are Pages)


begin
end.

Next We Will make Are Form


program AddingTabs;

// Adding Form :)
const
Amount = 10; // How Many Sheets


var
MainForm : TForm; // Are Main TForm
PageControl : TPageControl; // This is Are page Control Wich controls are tabs
TabPages : array [1..Amount] of TTabSheet;


procedure NotLoadForm;
begin
MainForm := CreateForm;
MainForm.SetBounds({left} 0, {Top} 0, {Width} 500, {Height} 500);
MainForm.Position := poDesktopCenter; // Will make it Start in the Desktop Center
end;

procedure NotShowForm;
begin
MainForm.ShowModal;
end;

procedure LoadForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotLoadForm', V); // Use Thread Safe call To make It Load Safely
end;

procedure ShowForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotShowForm', V); // Use Thread Safe call To make It Load Safely
end;


begin
try
LoadForm;
ShowForm;


finally
FreeForm(MainForm);


except
Writeln('Error: could Not Load Form');


end;
end.


Now That We Have Are Form Its Time to Put Are PageControl In!


program AddingTabs;
// Adding PageControl
const
Amount = 10; // How Many Sheets


var
MainForm : TForm; // Are Main TForm
PageControl : TPageControl; // This is Are page Control Wich controls are tabs
TabPages : array [1..Amount] of TTabSheet;


procedure NotLoadForm;
begin
MainForm := CreateForm;
MainForm.SetBounds({left} 0, {Top} 0, {Width} 500, {Height} 500);
MainForm.Position := poDesktopCenter; // Will make it Start in the Desktop Center

PageControl := TPageControl.Create(MainForm);
PageControl.Parent := MainForm;
PageControl.Align := alClient; // Make It Align With The Whole MainForm

end;

procedure NotShowForm;
begin
MainForm.ShowModal;
end;

procedure LoadForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotLoadForm', V); // Use Thread Safe call To make It Load Safely
end;

procedure ShowForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotShowForm', V); // Use Thread Safe call To make It Load Safely
end;


begin
try
LoadForm;
ShowForm;


finally
FreeForm(MainForm);


except
Writeln('Error: could Not Load Form');


end;
end.

Now That We Have Are PageControl in Lets Add Are Tabs!


program AddingTabs;

// Adding Pages
const
Amount = 10; // How Many Pages ;)


var
MainForm : TForm; // Are Main TForm
PageControl : TPageControl; // This is Are page Control Wich controls are tabs
TabPages : array [1..Amount] of TTabSheet;


procedure NotLoadForm;
var
i : Integer;
begin
MainForm := CreateForm;
MainForm.SetBounds({left} 0, {Top} 0, {Width} 500, {Height} 500);
MainForm.Position := poDesktopCenter; // Will make it Start in the Desktop Center

PageControl := TPageControl.Create(MainForm);
PageControl.Parent := MainForm;
PageControl.Align := alClient; // Make It Align With The Whole MainForm

for i := 1 to Amount do // Amount of Tabs
begin
TabPages[i] := TTabSheet.Create(MainForm);
TabPages[i].Caption := 'Tab ' + Inttostr(i); // The Caption of the title
TabPages[i].PageControl := PageControl; // We Must Identify are Page Control
end;

end;

procedure NotShowForm;
begin
MainForm.ShowModal;
end;

procedure LoadForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotLoadForm', V); // Use Thread Safe call To make It Load Safely
end;

procedure ShowForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotShowForm', V); // Use Thread Safe call To make It Load Safely
end;


begin
try
LoadForm;
ShowForm;


finally
FreeForm(MainForm);


except
Writeln('Error: could Not Load Form');


end;
end.


EXTRAS (Sender's)

Using OnShow And OnHide



program AddingTabs;

// Performing OnShow And OnHide

const
Amount = 10; // How Many Pages ;)


var
MainForm : TForm; // Are Main TForm
PageControl : TPageControl; // This is Are page Control Wich controls are tabs
TabPages : array [1..Amount] of TTabSheet;

procedure OnShow(Sender : TObject);
begin
TTabSheet(Sender).Caption := 'Current Tab'; // Identify Which Tab Is Currently on
end;

procedure OnHide(Sender : TObject);
begin // Change Tab To Its Olds Caption
TTabSheet(Sender).Caption := 'Tab ' + Inttostr(TTabSheet(Sender).PageIndex+1);
end;


procedure NotLoadForm;
var
i : Integer;
begin
MainForm := CreateForm;
MainForm.SetBounds({left} 0, {Top} 0, {Width} 500, {Height} 500);
MainForm.Position := poDesktopCenter; // Will make it Start in the Desktop Center

PageControl := TPageControl.Create(MainForm);
PageControl.Parent := MainForm;
PageControl.Align := alClient; // Make It Align With The Whole MainForm

for i := 1 to Amount do
begin
TabPages[i] := TTabSheet.Create(MainForm);
TabPages[i].Caption := 'Tab ' + Inttostr(i);
TabPages[i].PageControl := PageControl;
TabPages[i].OnShow := @OnShow;
TabPages[i].OnHide := @OnHide;
end;

end;

procedure NotShowForm;
begin
MainForm.ShowModal;
end;

procedure LoadForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotLoadForm', V); // Use Thread Safe call To make It Load Safely
end;

procedure ShowForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotShowForm', V); // Use Thread Safe call To make It Load Safely
end;


begin
try
LoadForm;
ShowForm;


finally
FreeForm(MainForm);


except
Writeln('Error: could Not Load Form');


end;
end.


Make Tabs Switchable!!!
in order to switch the tab you must hold Ctrl while dragging it to the other tab.



program AddingTabs;

// Able To Move Tabs Around!!!!

const
Amount = 10;


var
MainForm : TForm; // Are Main TForm
PageControl : TPageControl; // This is Are page Control Wich controls are tabs
TabPages : array [1..Amount] of TTabSheet;




procedure OnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if (ssCtrl in Shift) then PageControl.BeginDrag(False); // if Ctrl hold down then Start Drag
end;

procedure OnDragDrop(Sender, Source: TObject; X, Y: Integer);
var
i : Integer;
begin
PageControl.ActivePage.PageIndex := PageControl.IndexOfTabAt(X, Y);
end;

procedure OnDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean) ;
begin
Accept := (Sender is TPageControl);
end;


procedure NotLoadForm;
var
i : Integer;
begin
MainForm := CreateForm;
MainForm.SetBounds({left} 0, {Top} 0, {Width} 500, {Height} 500);
MainForm.Position := poDesktopCenter; // Will make it Start in the Desktop Center

PageControl := TPageControl.Create(MainForm);
PageControl.Parent := MainForm;
PageControl.Align := alClient; // Make It Align With The Whole MainForm
PageControl.OnMouseDown := @OnMouseDown;
PageControl.OnDragDrop := @OnDragDrop;
PageControl.OnDragOver := @OnDragOver;

for i := 1 to Amount do
begin
TabPages[i] := TTabSheet.Create(MainForm);
TabPages[i].Caption := 'Tab ' + Inttostr(i);
TabPages[i].PageControl := PageControl;
end;


end;

procedure NotShowForm;
begin
MainForm.ShowModal;
end;

procedure LoadForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotLoadForm', V); // Use Thread Safe call To make It Load Safely
end;

procedure ShowForm;
var
V : TVariantArray;
begin
V := []; // Set Array 0
ThreadSafeCall('NotShowForm', V); // Use Thread Safe call To make It Load Safely
end;


begin
try
LoadForm;
ShowForm;


finally
FreeForm(MainForm);


except
Writeln('Error: could Not Load Form');


end;
end.



Town

2007-07-15 19:00:26

Wooooooooooo, I was waiting for this.

Any chance you could give me a quick explaination on:

MainImage:= TImage.Create(MainForm);
MainImage.parent:=MainForm;
MainImage.SetBounds(0, 0, 500, 400);
MainImage.Canvas.Pen.Width := 90;
For C:= 1 to 60 do
begin
radius:= radius + 5;
MainImage.CANVAS.Pen.COLOR := MainImage.CANVAS.Pen.COLOR+3;
For i:= 1 to 36 do // Draw Back Ground
begin
Radial := Radial + 10;
x:= Round(radius * Sin((Pi / 180) * Radial ) + 255);
y:= Round(-radius * Cos((Pi / 180) * Radial ) + 200);
MainImage.CANVAS.MoveTo(x, y);
MainImage.CANVAS.LineTo(x, y);
end;
Radial:= 0;
Wait(1);
end;

I somewhat understand TImages but I'm not sure exactly how that last thing works (mainly because the math is a bit out of my range).

SKy Scripter

2007-07-15 19:03:00

Hah yeah that codes mine :p

basically what that does is draw a pretty cricle :)

bullzeye95

2007-07-15 19:17:05

Could you explain the in keyword please? And TShiftState? I search for delphi in on Google, but I get a bunch of maps :p

Santa_Clause

2007-07-15 19:27:42

Woot…nice tutorial. Rep++

SKy Scripter

2007-07-15 19:28:00

T Shift State is a Type

and basically what "in" does is it return whether you have ssAlt or
ssShift and ect.. In TShiftState (Kinda Like Having a KeyDown in the Same Procedure) i really cant explain it better then that lolz

Here

2007-07-15 19:29:49

Amazing thing to know. Thanks.

itSchRis917

2007-07-15 23:11:18

Very nice tutorial. I was wondering how to do this..

itSchRis917

2007-07-16 15:13:22

Why the hell do i always get an unknown identifier error "caption"?? isnt that just a string? even if i declare it as a string.. it still doesnt work. Help please!

SKy Scripter

2007-07-16 15:33:40

From: itschris917
Why the hell do i always get an unknown identifier error "caption"?? isnt that just a string? even if i declare it as a string.. it still doesnt work. Help please!


i sent you a pm (you need the newer SCAR)

itSchRis917

2007-07-16 15:43:06

Thanks Sky Scripter, it works now. :)

ronny.m.p

2007-08-03 08:53:52

Nice! How do we go about putting buttons and edit boxs etc. on a different tab then our main tab?

Starblaster100

2007-08-03 09:00:25

Create the button as normal, but change the Parent to TabPages[1];

Miitchyy

2007-08-03 09:11:43

Yep, and if you want a canvas since TabPages dont have em you needa make a TImage and use that :P

Nava2

2008-09-07 22:46:54

Great tut, helped a lot! :)

Shuttleu

2009-01-31 12:22:06

Thank you soo much
I can finally have some tabs in my forms :D

~shut