SRL Archive - OSR Advanced Scripting Tutorials

How to use tags in forms!


itSchRis917

2007-07-18 23:13:57

This is my first tut, so sorry if i didnt explain it clearly.

Ok, so I'm going to teach you how to use tags in forms. So, tags are used in forms for mainly one purpose: to save code. When using .tag it allows you to set multiple TObject's to seperate procedures all within the same ClickButton procedure.

So, here would be the simple, sender: TObject procedure using tags.


procedure clickbutton(sender: tobject);
var
v: TVariantArray;
begin
SetArrayLength(V, 0);
if (Sender is TButton) then
case (TButton(Sender).tag) of // used like any other case ____ of statement
-1: begin
PlayerClicked := true;
ThreadSafeCall('SetupForm', v);
end;
-2: frmDesign.ModalResult := mrOk;
-3: begin
frmDesign.ModalResult := mrOk;
Writeln('Script canceled.');
//FreeForm(frmDesign);
TerminateScript;
end;
-4: begin
ColorsClicked := true;
LoadColorsForm;
end;
-5: LoadHelpForm;
-6: LoadStatsForm;
end;
end;

So the majority of that code, is made up of Case(TButton(Sender).tag) of, which is just a basic Case..Of statement, which you should probably know how to use.

BUT…
How do you use the .tag variable?? Simple. You declare it like you would declare any other form variable.

PlayersButton.Tag := -1;
And so on… But I'm not going to go into greater detail than that..

Here's part of my form that i took it from. I'm not going to post the whole form though…

PlayersButton := TButton.Create(frmDesign);
PlayersButton.Parent := frmDesign;
PlayersButton.Left := 190;
PlayersButton.Top := 5;
PlayersButton.Width := 75;
PlayersButton.Height := 25;
PlayersButton.Caption := 'Edit Players';
PlayersButton.OnClick := @clickbutton;
PlayersButton.Tag := -1;
PlayersButton.TabOrder := 8;

StartButton := TButton.Create(frmDesign);
StartButton.Parent := frmDesign;
StartButton.Left := 190;
StartButton.Top := 92;
StartButton.Width := 75;
StartButton.Height := 25;
StartButton.Caption := 'Start Script';
StartButton.OnClick := @clickbutton;
StartButton.Tag := -2;
StartButton.TabOrder := 9;

CancelButton := TButton.Create(frmDesign);
CancelButton.Parent := frmDesign;
CancelButton.Left := 190;
CancelButton.Top := 120;
CancelButton.Width := 75;
CancelButton.Height := 25;
CancelButton.Caption := 'Cancel';
CancelButton.OnClick := @clickbutton;
CancelButton.Tag := -3;
CancelButton.TabOrder := 10;

ColorsButton := TButton.Create(frmDesign);
ColorsButton.Parent := frmDesign;
ColorsButton.Left := 190;
ColorsButton.Top := 33;
ColorsButton.Width := 75;
ColorsButton.Height := 25;
ColorsButton.Caption := 'Edit Colors';
ColorsButton.OnClick := @clickbutton;
ColorsButton.Tag := -4;
ColorsButton.TabOrder := 11;

HelpButton := TButton.Create(frmDesign);
HelpButton.Parent := frmDesign;
HelpButton.Left := 190;
HelpButton.Top := 63;
HelpButton.Width := 75;
HelpButton.Height := 25;
HelpButton.Caption := 'Help';
HelpButton.OnClick := @clickbutton;
HelpButton.Tag := -5;
HelpButton.TabOrder := 13;

StatsButton := TButton.Create(frmDesign);
StatsButton.Parent := frmDesign;
StatsButton.Left := 115;
StatsButton.Top := 120;
StatsButton.Width := 75;
StatsButton.Height := 25;
StatsButton.Caption := 'View Stats';
StatsButton.OnClick := @clickbutton;
StatsButton.Tag := -6;
StatsButton.TabOrder := 16;

So, thats about all there is to it. Hope you enjoyed it!

~ itschris917

jhildy

2007-07-19 15:13:12

Whoa thats pretty cool iv never thought of that ill have to try it out thanks.

Santa_Clause

2007-07-24 23:26:29

Interesting. Easier than all those procedures that I make…

itSchRis917

2007-07-25 09:52:03

^^ Yea I did the same thing before.. @buttonclick. @editplayers. @.. @….. And then i always had to remember what was which and blah blah. Not cool.

Jason2gs

2007-07-25 12:38:18

Oooh…

Thank you for this :)

itSchRis917

2007-07-25 17:48:16

No problem. Anyone else like it? ;)

WhiteShadow

2007-07-25 17:54:56

You can just use:

procedure THEEClick(sender: TObject);
begin
case Sender of
VarButtonName: DoSomething;
VarMemoName: Dosttuf;
EditName: dostuuf;
end;
end;

?

itSchRis917

2007-07-26 01:23:54

Yea that's essentially what i did also. I only knew how to do it using .tag.

brad734

2008-09-22 02:12:23

doh.. shouolda thoguth of that sooner