Page 1 of 6 123 ... LastLast
Results 1 to 25 of 126

Thread: THE big form tutorial ~ by MK

  1. #1
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default THE big form tutorial ~ by MK


    THE big form tutorial ~ by MasterKill

    Some people might have noticed that I like to make nice forms for the setup of my scripts. I’m going to teach you guys how it’s done, and how you can make a nice form yourself.

    This is what you should be able to make at the end of the tutorial:

    Components

    Player form

    Advanced components


    I hope you'll learn a lot from it and that it gives you an nice result!

    Good luck!

    Wall of fame:

    Quote Originally Posted by Richard View Post


    Been working on that for quite a while, still a flaw with the RadioButton, should sort it soon.
    Quote Originally Posted by mormonman View Post
    All learned from this tutorial:


    Thanks mucho... never really learned this stuff until now. Easier than I thought it would be. xD Maybe finish the player array stuff... I might try my hand at it on a 3 hour drive home I got tomorrow though.
    Quote Originally Posted by noidea View Post


    MasterKill: Adding and delting players, ect Tabs (Thx to you)
    Thanks so much. I made this about a week ago on like 3 car trips. tysm
    Quote Originally Posted by NaumanAkhlaQ View Post



    Quote Originally Posted by NaumanAkhlaQ View Post
    I learned it from this tut, well the TImage part
    Last edited by MasterKill; 08-04-2009 at 06:56 PM.

  2. #2
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    TForm;

    The first step of making your form, is the TForm; We will call our form frmDesign, wish is most used. Before you can start working on your form, you need some standard functions for creating your form, and making it visible. The “standard stuff”:
    SCAR Code:
    var
      frmDesign: TForm;

    procedure InitForm;
    begin
      // This is where you'll build your form
    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;        // those 2 function are the action form setup. Thise one creates your form
      SafeShowFormModal;  // and this one makes it visible
    end.
    Now, you don’t have a working form yet. To get our “FORM” we are going to create our background. The big base of the form.
    SCAR Code:
    procedure InitForm;
    begin
      frmDesign := CreateForm;          // This will create your form. MUST be called first.
      frmDesign.Left := 100;            // How many pixels from the left must the form be vissible?
      frmDesign.Top := 100;             // How many pixels from the top must the form be vissible?
      frmDesign.Width := 500;           // The width of you form in pixels
      frmDesign.Height := 500;          // The height of you form in pixels
      frmDesign.Caption := 'Test!';     // The name of your form? This will be shown in the upper left balk
      frmDesign.Color := ClWhite;       // The background color of your form ("ClWhite", "ClBlack" ect, or just an color like: 123456)
    end;
    Take a good look at those, this will be the base of your form. Play around a lil with those vars and see what they do. Don’t forget to add the “standard stuff” that I showed earlier!

    If you want to go more advanced, you can do more stuff with the TForm. Decide the “standard” color for you text(TLabel) in the form, or what font it should be.
    SCAR Code:
    frmDesign.Font.Color := ClBlack;        // The color of the text (Tlabels) when you have those
      frmDesign.Font.Name := 'Comic Sans MS'; // The name of the font you want the Tlabels to be

    Congratulations! You now know how to make the very basics of a form!

    Advanced TForm;

    If you don’t like the “close” and “minisize” button in the upper right corner you could add the following code to your form, wish is OPTIONAL, if you want to hide those.
    SCAR Code:
    frmDesign.BorderStyle := bsNone;
    NOTE: If you use this, you should make a CLOSE button! I’ll learn you later in the tutorial how to make one. But for now, if you want to test it, Press “ALT + F4” to close the form and stop the script.

    FAQ TForm;

    no questions yet.

    What do we have now?

    SCAR Code:
    var
      frmDesign: TForm;

    procedure InitForm;
    begin
      frmDesign := CreateForm;          // This will create your form. MUST be called first.
      frmDesign.Left := 100;            // How many pixels from the left must the form be vissible?
      frmDesign.Top := 100;             // How many pixels from the top must the form be vissible?
      frmDesign.Width := 500;           // The width of you form in pixels
      frmDesign.Height := 500;          // The height of you form in pixels
      frmDesign.Caption := 'Test!';     // The name of your form? This will be shown in the upper left balk
      frmDesign.Color := ClWhite;       // The background color of your form ("ClWhite", "ClBlack" ect, or just an color like: 123456)
      frmDesign.Font.Color := ClBlack;        // The color of the text (Tlabels) when you have those
      frmDesign.Font.Name := 'Comic Sans MS'; // The name of the font you want the Tlabels to be
    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;        // those 2 function are the action form setup. Thise one creates your form
      SafeShowFormModal;  // and this one makes it visible
    end.

    End of TForm;

  3. #3
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    TButton;

    Before you start you should know how the TForm; works ans how to make use of it.

    Adding an button to your form. Might not sound that hard, but behind a button is a procedure. Cause, if you click on the button you want something to happen. First we are going to add our button to the global vars.
    SCAR Code:
    var
      frmDesign: TForm;
      Button1: TButton; // there we go
    Now, we have to “create” our button in the form, wich will go like the following:
    SCAR Code:
    Button1 := TButton.Create(FrmDesign);   // This will create the button. Must be called.
      Button1.Parent := FrmDesign;            // The parent is very important. Without the parrent set to the formname the button won't be visible
      Button1.Left := 10;                     // how many pixels from the left
      Button1.Top := 425;                     // how many pixels from the top
      Button1.Height := 30;                   // the height of the button
      Button1.Width := 100;                   // the width of the button
      Button1.Caption := 'Close form';        // The text on the button!
    Run your script, and you see your shiny button. But, when you click on it, nothing is happening! That’s right, nothing happends. To let the form know what to do you have to add an “OnClick” option. Note: the procedure for the button is a bit different then other procedures, and it will only work if you call it this way:
    Add this to your form setup:
    SCAR Code:
    Button1.OnClick := @ButtonClick;     //Dont forget the @
    And make this new procedure above your form setup:
    SCAR Code:
    Procedure ButtonClick(Sender: TObject);
    Begin
       // what to do if the button is clicked!
    End;
    Of course you can rename the procedure to whatever you like. But what is the (Sender: TObject);. That is for the script to know that the procedure is called by a TObject. A TObject can be anything form your form. A TButton, TLabel, TEdit, ect. The TObject where you clicked on will be returned as the Sender. And In this case the sender will be Button1.

    Now, when you click the button, still nothing is happening. But it sure is calling your procedure I’ll tell you! Just add some WriteLn(‘’) to your procedure to see if it’s working.

    Now, we called our button ‘Close form’. To make that actually happening, you will have to add the following code to your ButtonClick procedure:
    SCAR Code:
    Procedure ButtonClick(Sender: TObject);
    Begin
      WriteLn(‘closing form!’);
      frmDesign.ModalResult:= mrOk;
    End;
    When you have called that line, your form will close and your script will resume.

    Congratulations! You now know how to add buttons to your form!

    Advanced TButton;

    When you make more then 1 button, I suggest you put those in an array! This will shorten your code up a lot and it looks more advanced, but easier to overview though!

    Small example:
    SCAR Code:
    Var
      Buttons: Array [0..1] Of TButton; // the var you need to add
       Index: Integer; // we need this for you loop;

    // the onclick procedure:

    Procedure ButtonClick(Sender: TObject);
    Begin
      Case Sender Of
        Buttons[0]: Begin
          WriteLn(‘start!’);
        End;
        Buttons[1]: Begin
          WriteLn(‘stop!’);
        End;
      End;
    End;

    // the following in your form setup:

      For Index := 0 To 1 Do
      Begin
        Buttons[Index] := TButton.Create(FrmDesign);
        Buttons[Index].Parent := FrmDesign;
        Buttons[Index].Height := 20;
        Buttons[Index].Width := 100;
        Buttons[Index].Left := 10;
        Buttons[Index].Top := 10 + (Index * 30); //this will but the buttons in a nice line
        Buttons[Index].OnClick := @ButtonClick;
      End;
      Buttons[0].Caption := ‘Start’;
      Buttons[1].Caption := ‘Stop’; // you can also make a string array of those and put this in your loop to.

    FAQ TButton;

    no questions yet.

    What do we have now?

    SCAR Code:
    var
      frmDesign: TForm;
      Button1: TButton;
     
    Procedure ButtonClick(Sender: TObject);
    Begin
      WriteLn('closing form!');
      frmDesign.ModalResult:= mrOk;
    End;

    procedure InitForm;
    begin

      { The TForm; part }

      frmDesign := CreateForm;          // This will create your form. MUST be called first.
      frmDesign.Left := 100;            // How many pixels from the left must the form be vissible?
      frmDesign.Top := 100;             // How many pixels from the top must the form be vissible?
      frmDesign.Width := 500;           // The width of you form in pixels
      frmDesign.Height := 500;          // The height of you form in pixels
      frmDesign.Caption := 'Test!';     // The name of your form? This will be shown in the upper left balk
      frmDesign.Color := ClWhite;       // The background color of your form ("ClWhite", "ClBlack" ect, or just an color like: 123456)
      frmDesign.Font.Color := ClBlack;        // The color of the text (Tlabels) when you have those
      frmDesign.Font.Name := 'Comic Sans MS'; // The name of the font you want the Tlabels to be
     
      { The TButton; part }
     
      Button1 := TButton.Create(FrmDesign);   // This will create the button. Must be called.
      Button1.Parent := FrmDesign;            // The parent is very important. Without the parrent set to the formname the button won't be visible
      Button1.Left := 10;                     // how many pixels from the left
      Button1.Top := 425;                     // how many pixels from the top
      Button1.Height := 30;                   // the height of the button
      Button1.Width := 100;                   // the width of the button
      Button1.Caption := 'Close form';        // The text on the button!
      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
      SafeInitForm;        // those 2 function are the action form setup. Thise one creates your form
      SafeShowFormModal;  // and this one makes it visible
    end.

    End of TButton;

  4. #4
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    TLabel;

    Fist of all, what are TLabels exactly. A TLabel is a line of text sort of implanted to the background. Other people can't change the text while the form is open but you can with some code.

    So lets add our TLabel to the vars (We will use the script from the TButton tutorial!)
    SCAR Code:
    var
      frmDesign: TForm;
      Button1: TButton;
      Label1: TLabel;
    Now, to add the label to your form, you have to add this to your form setup:
    SCAR Code:
    Label1 := TLabel.Create(FrmDesign);     // This will create the label. Must Be called.
      Label1.Parent := FrmDesign;             // The parent is very important. Without the parrent set to the formname the label wont be visible
      Label1.Top := 10;                       // how many pixels from the left
      Label1.Left := 10;                      // how many pixels from the top
      Label1.Caption := 'A form tut label!';  // What does your label need to say?
    Your text seems to be a bit small, and you don't like the font to much. To edit the font you add this to your script:
    SCAR Code:
    Label1.Font.Size := 18;                 // The size of your text
      Label1.Font.Color := ClRed;             // The color of your text
      Label1.Font.Name := 'Comic Sans MS';    // The font name of your text
    Fun a bit around with those variables to know what there exactly doing.

    You can use labels for your credits, but you can also change the labels name when some one presses a button for example.

    If you take a good look at this code and run the script, you can exactly see how it's done:
    SCAR Code:
    var
      frmDesign: TForm;
      Button1: TButton;
      Label1: TLabel;
     
      Count: Integer; // This variable will be used for the counting!
     
    Procedure ButtonClick(Sender: TObject);
    Begin
      Count := Count + 1;
     
      Label1.Caption := 'You currently have: ' + IntToStr(Count); // this is where it updates the labels text when the button is pressed.
     
    End;

    procedure InitForm;
    begin

      { The TForm; part }

      frmDesign := CreateForm;
      frmDesign.Left := 100;
      frmDesign.Top := 100;
      frmDesign.Width := 500;
      frmDesign.Height := 500;
      frmDesign.Caption := 'Test!';
      frmDesign.Color := ClWhite;
      frmDesign.Font.Color := ClBlack;
      frmDesign.Font.Name := 'Comic Sans MS';
     
      { The TButton; part }
     
      Button1 := TButton.Create(FrmDesign);
      Button1.Parent := FrmDesign;
      Button1.Left := 10;
      Button1.Top := 425;
      Button1.Height := 30;
      Button1.Width := 100;
      Button1.Caption := '+ 1!';
      Button1.OnClick := @ButtonClick;
     
      { The TLabel; Part }
     
      Label1 := TLabel.Create(FrmDesign);     // This will create the label. Must Be called.
      Label1.Parent := FrmDesign;             // The parent is very important. Without the parrent set to the formname the label wont be visible
      Label1.Top := 10;                       // how many pixels from the left
      Label1.Left := 10;                      // how many pixels from the top
      Label1.Caption := 'You currently have: 0';// What does your label need to say?
      Label1.Font.Size := 18;                 // The size of your text
      Label1.Font.Color := ClRed;             // The color of your text
      Label1.Font.Name := 'Comic Sans MS';    // The font name of your text
    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.

    Congratulations! You now know how to add labels to your form!

    Advanced TLabel;

    When you have more then one label, it's nice to put your labels in an array. This will make you code shorter and easy to overview your form.

    Here is some small code to see how it's done:

    SCAR Code:
    Var
      Labels: Array [0..5] Of TLabel;
      LabelName: Array Of String;
      Index: Integer; // needed for the loop


    // this should be in your form setup:

      LabelName := ['first label text', 'second label text', 'third label text', 'fourth label text', 'fifth label text', 'last label text'];
      For Index := 0 To 5 Do
      Begin
        Labels[Index] := TLabel.Create(FrmDesign);
        Labels[Index].Parent := FrmDesign;
        Labels[Index].Left := 10;
        Labels[Index].Top := 10 + (30 * Index); // this will nicely line it up
        Labels[Index].Caption := LabelName[Index];
      End;

    FAQ TLabel;

    no questions yet.

    End of TLabel;

  5. #5
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    TEdit;

    A TEdit, is a place where users can put something in. Your name for example. In this part I'll teach how to make and use a TEdit; Before you start on the TEdit, you should know how to use the TForm, TLabel and TButton already.

    Lets add our TEdit to the global variables.
    SCAR Code:
    Var
      Edit1: TEdit;
    Once you have that, you have to actual create the Editbox in the box. You do that by adding the following code to your form setup:
    SCAR Code:
    Edit1 := TEdit.Create(FrmDesign);   // This will create the editbox. Must be called.
      Edit1.Parent := FrmDesign;          // without this your editbox wont be visible
      Edit1.Top := 30;                    // how many pixels from the top
      Edit1.Left := 10;                   // how many pixels from the left
      Edit1.Width := 350;                 // the width of your editbox
      Edit1.Height := 20;                 // the height of you editbox
    There, you now have a shiny TEdit. Now, to actual let it do something.

    If you want to have some text in the box, you can add this:
    SCAR Code:
    Edit1.Text := 'enter your name here';
    Now, when you open your form again you'll see your text in the box. This can be edited by the user to whatever he wants. Reading what's in the box goes the same.

    Now, lets make a form where an TEdit is needed.
    SCAR Code:
    var
      frmDesign: TForm;
      Button1: TButton;
      Label1: TLabel;
      Edit1: TEdit;

    Procedure ButtonClick(Sender: TObject);
    Begin
      frmDesign.ModalResult:= mrOk;
      ShowMessage('Hello ' + Edit1.Text + '!'); // here you see that it will call your name, wich you putted in the TEdit;
    End;

    procedure InitForm;
    begin

      { The TForm; part }

      frmDesign := CreateForm;
      frmDesign.Left := 100;
      frmDesign.Top := 100;
      frmDesign.Width := 500;
      frmDesign.Height := 100;
      frmDesign.Caption := 'Test!';
      frmDesign.Color := ClWhite;
      frmDesign.Font.Color := ClBlack;
      frmDesign.Font.Name := 'Comic Sans MS';

      { The TButton; part }

      Button1 := TButton.Create(FrmDesign);
      Button1.Parent := FrmDesign;
      Button1.Left := 375;
      Button1.Top := 30;
      Button1.Height := 20;
      Button1.Width := 100;
      Button1.Caption := 'OK';
      Button1.OnClick := @ButtonClick;

      { The TLabel; Part }

      Label1 := TLabel.Create(FrmDesign);
      Label1.Parent := FrmDesign;
      Label1.Top := 10;
      Label1.Left := 10;
      Label1.Caption := 'what is your name?';
     
      { The TEdit; Part }
     
      Edit1 := TEdit.Create(FrmDesign);   // This will create the editbox. Must be called.
      Edit1.Parent := FrmDesign;          // without this your editbox wont be visible
      Edit1.Top := 30;                    // how many pixels from the top
      Edit1.Left := 10;                   // how many pixels from the left
      Edit1.Width := 350;                 // the width of your editbox
      Edit1.Height := 20;                 // the height of you editbox
    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.

    Congratulations! You now know how to add editboxes to your form!

    Advanced TEdit;

    You can limit users in what to put in the TEdit. Scar has the following properties:

    • SCAR Code:
      .MaxLength := 4;
      The user will not be able to put more then 4 chars in your editbox now. Usefull for Players[].Pin for example.
    • SCAR Code:
      .PasswordChar := '*';
      This is very useful to hide passwords! if you type: P@SSW0RD, you will only see "********", but when you get the text of your box (string := Edit.Text) you'll get P@SSW0RD again


    FAQ TEdit;

    no questions yet.

    End of TEdit;

  6. #6
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    TComboBox;

    A TComboBox is almost the same as a TEdit;. Except, you can add "choose able" options to your combo box. A sort of option list. In this tutorial part I'll show you how to made a TComboBox and add Items to your "option list".

    First of all, lets add the TComboBox to your global vars.
    SCAR Code:
    Var
      ComboBox1: TComboBox;
    Once you have that, you have to actual create the combobox in the form. You do that by adding the following code to your form setup:
    SCAR Code:
    ComboBox1 := TComboBox.Create(FrmDesign); // This will create the combobox. Must be called.
      ComboBox1.Parent := FrmDesign;          // without this your combobox wont be visible
      ComboBox1.Top := 30;                    // how many pixels from the top
      ComboBox1.Left := 10;                   // how many pixels from the left
      ComboBox1.Width := 350;                 // the width of your combobox
      ComboBox1.Height := 20;                 // the height of you combobox
    When you add this to your form, you'll see a box pretty much the same as an TEditBox; Now, you wand to add options to your combobox. (that's where you made it for ). To add Items to your combobox add the following to your form setup:
    SCAR Code:
    ComboBox1.Items.Add('Cookies');
      ComboBox1.Items.Add('Pizza');
      ComboBox1.Items.Add('Taco''s');
      ComboBox1.Items.Add('Coca cola light');
    When you run the form again, you'll see that your options are visible! Also, the same as an TEdit; the text that's in the box can be found with:
    SCAR Code:
    ComboBox.Text := 'Insert food here';
      WriteLn(ComboBox1.Text);
    To see an preview of the ComboBox in an working form:
    SCAR Code:
    var
      frmDesign: TForm;
      Button1: TButton;
      Label1: TLabel;
      ComboBox1: TComboBox;

    Procedure ButtonClick(Sender: TObject);
    Begin
      frmDesign.ModalResult:= mrOk;
      ShowMessage('You like ' + ComboBox1.Text + ' the most!'); // here you see that it will call your name, wich you putted in the TEdit;
    End;

    procedure InitForm;
    begin

      { The TForm; part }

      frmDesign := CreateForm;
      frmDesign.Left := 100;
      frmDesign.Top := 100;
      frmDesign.Width := 500;
      frmDesign.Height := 100;
      frmDesign.Caption := 'Test!';
      frmDesign.Color := ClWhite;
      frmDesign.Font.Color := ClBlack;
      frmDesign.Font.Name := 'Comic Sans MS';

      { The TButton; part }

      Button1 := TButton.Create(FrmDesign);
      Button1.Parent := FrmDesign;
      Button1.Left := 375;
      Button1.Top := 30;
      Button1.Height := 20;
      Button1.Width := 100;
      Button1.Caption := 'OK';
      Button1.OnClick := @ButtonClick;

      { The TLabel; Part }

      Label1 := TLabel.Create(FrmDesign);
      Label1.Parent := FrmDesign;
      Label1.Top := 10;
      Label1.Left := 10;
      Label1.Caption := 'What do you like most?';

      { The TComboBox; Part }

      ComboBox1 := TComboBox.Create(FrmDesign); // This will create the combobox. Must be called.
      ComboBox1.Parent := FrmDesign;          // without this your combobox wont be visible
      ComboBox1.Top := 30;                    // how many pixels from the top
      ComboBox1.Left := 10;                   // how many pixels from the left
      ComboBox1.Width := 350;                 // the width of your combobox
      ComboBox1.Height := 20;                 // the height of you combobox
      ComboBox1.Items.Add('Cookies');
      ComboBox1.Items.Add('Pizza');
      ComboBox1.Items.Add('Taco''s');
      ComboBox1.Items.Add('Coca cola light');
    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.
    The last thing you should know about TComboBoxes is how to clear the option items. You remove all your added items with the following code:
    SCAR Code:
    ComboBox1.Clear;

    Congratulations! You now know how to add ComboBoxes to your form!


    Advanced TComboBox;

    The advanced stuff of a TComboBox, is actually the same as the TEdit. You can limit users in what to put in the ComoBox. Scar has the following properties:

    • SCAR Code:
      .MaxLength := 4;
      The user will not be able to put more then 4 chars in your combobox now. Useful for Players[].Pin for example.
    • SCAR Code:
      .PasswordChar := '*';
      This is very useful to hide passwords! if you type: P@SSW0RD, you will only see "********", but when you get the text of your box (string := Edit.Text) you'll get P@SSW0RD again


    FAQ TComboBox;

    no questions yet.

    End of TComboBox;

  7. #7
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    TListBox;

    The following might sound a bit obvious, but I can't give it an other discription. A TListBox is a box with non-user-edit able items in it. But the great thing is, that you can make a onclick procedure after the box, and preform an action when a item in the list is clicked. Or you can just use it to load your "last saved progress report" in for example. We will start of with the basic creating, then how to add a report in it, then how to make the onclick work.

    And of course you'll first need to add the box to your global vars:
    SCAR Code:
    Var
      List1: TListBox;
    And this to actual create the list in your form:
    SCAR Code:
    List1 := TListBox.Create(FrmDesign); // This creates the ListBox. Must be called
      List1.Parent := FrmDesign;           // Without this set to the form the list wont be vissible
      List1.Top := 30;                     // how many pixels from the top
      List1.Left := 10;                    // how many pixels from the left
      List1.Width := 250;                  // whats the width of your list?
      List1.Height := 220;                 // whats the height of your list?
    There we have our list. Nice and empty. So now, lets say you want to add a progress report to your list. You'll have to add the following to your form setup:
    SCAR Code:
    List1.Items.Add('MasterKills form tutorial!');
      List1.Items.Add('');
      List1.Items.Add('  helped much people.');
    And now, you see in your list the lines you added when you run the form.
    If you make the list longer then the hight is, don't worry, a scrollbar will appear.

    The last basic thing you need to know is how to make the list empty again if needed. You do that by simply adding .Clear;
    SCAR Code:
    List1.Clear;

    Now, on how to see on what Item you clicked at your list, you need to add the onclick procedure.

    Add this above your form setup:
    SCAR Code:
    Procedure ListClick(Sender: TObject);
    Begin

    End;
    And this in your form setup:
    SCAR Code:
    List1.OnClick := @ListClick;
    That will call procedure ListClick when you clicked on it. Now, to know what item of the list you clicked, you have List1.ItemIndex;. this variable contains the integer of the selected item in the list. If nothing is selected it will result -1; To see what you clicked you add this code to your ListClick procedure:
    SCAR Code:
    ShowMessage('You clicked at item: ' + IntToStr(List1.ItemIndex));
    Congratulations! You now know how to add a list to your form!

    An other example of a working list:

    SCAR Code:
    var
      frmDesign: TForm;
      Label1: TLabel;
      List1: TListBox;

    Procedure ListClick(Sender: TObject);
    Begin
      Label1.Caption := 'Last clicked box item: ' + IntToStr(List1.ItemIndex);
    End;

    procedure InitForm;
    begin
      { The TForm; part }

      frmDesign := CreateForm;
      frmDesign.Left := 100;
      frmDesign.Top := 100;
      frmDesign.Width := 300;
      frmDesign.Height := 300;
      frmDesign.Caption := 'Test!';
      frmDesign.Color := ClWhite;
      frmDesign.Font.Color := ClBlack;
      frmDesign.Font.Name := 'Comic Sans MS';

      { The TLabel; Part }

      Label1 := TLabel.Create(FrmDesign);
      Label1.Parent := FrmDesign;
      Label1.Top := 10;
      Label1.Left := 10;
      Label1.Caption := 'Last clicked box item:';

      { The TListBox; Part }

      List1 := TListBox.Create(FrmDesign); // This creates the ListBox. Must be called
      List1.Parent := FrmDesign;           // Without this set to the form the list wont be vissible
      List1.Top := 30;                     // how many pixels from the top
      List1.Left := 10;                    // how many pixels from the left
      List1.Width := 250;                  // whats the width of your list?
      List1.Height := 220;                 // whats the height of your list?
      List1.Items.Add('first info line');
      List1.Items.Add('second info line');
      List1.Items.Add('third info line');
      List1.OnClick := @ListClick;
    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.

    Advanced TListBox;

    under construction

    FAQ TListBox;

    no questions yet.

    End of TListBox;

  8. #8
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    TImage

    TImage is a fun one, It allows you to place a picture in your form. Or at least, that is the part I'll teaching you.

    To start, add the TImage var to your script, BUT, you also have to add some other variables to your form. You need 3 integers to make your image work. We will add b, w and h.
    SCAR Code:
    Var
      Image1: TImage;
      b, w, h: Integer;

    now, get the image you want to put into your form.
    To add the image to your form add this to your form setup:
    SCAR Code:
    Image1 := TImage.Create(FrmDesign);
      Image1.Parent := FrmDesign;
      Image1.Left := 20;
      Image1.Top := 20;
      Image1.Width := {width of your image};
      Image1.Height := {height of your image};
      b := loadbitmap({Location of your file as a string! (C:\Program Files\?? ect...});
      getbitmapsize(b, w, h);
      copycanvas(getbitmapcanvas(b), Image1.canvas, 0, 0, w, h, 0, 0, w, h);
    When you have done this, and replaced the {...} stuff with what should be there, try to run the form and you'll see your image in the form

    Congratulations! You now know how to add a picture to your form!

    If you think the standard colored background of your form is boring, you could make a TImage as background! If you want to use a TImage as background, your form setup order should be like this:
    • First setup your TForm;
    • Then setup your TImage for the background
    • Then setup all your other buttons, labels, edits, ect ect.


    Advanced TImage;

    under construction

    FAQ TImage;

    no questions yet.

    End of TImage;

  9. #9
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    TTimer;



    A TTimer, as you can see in the image above, a TTimer will call a function every set amount of time. This can be usefull for for changing the image of you form every few seconds, count up/down, and lots of other stuff. In this part of the tutorial I'm going to learn you how to make a TTimer, and how to make good use of it.

    The first thing you should know, a TTimer is not visible.

    Lets add the var to the script:
    SCAR Code:
    Var
      Time1: TTimer;

    Since your TTimer is not visible, you don't have to set the width, height, top, left and the parent. But there are a few other things you have to call in your form setup:

    SCAR Code:
    Timer1 := TTimer.Create(FrmDesign); // This will create your timer. Must be called.
      Timer1.Interval := 1000;            // After how many mili-seconds must your procedure be called?
      Timer1.OnTimer := @OnTimer;         // Do what procedure if your timer is ready?
      Timer1.Enabled := True;             // Active timer? True for yes.

    As you might already have noticed: The timer wants to call a procedure. So lets make it! Place this ABOVE your form setup
    SCAR Code:
    Procedure OnTimer(Sender: TObject);
    Begin
      WriteLn('Hello!');
    End;
    When you run your script, you'll see that every second a new "Hello!" will be added to your debug

    Note: when you close your form, the Timer WILL NOT STOP. So, after your form, you should call:
    SCAR Code:
    Timer1.Enabled := False;
    That will deactivate your timer and your script can play.

    Important:
    • Do never add a mouse movement in your TTimer procedure. You script will crash if scar has to do 2 mouse movements at the same time.
    • Make sure you stop the timer once the form is closed.


    Another TTimer preview:

    SCAR Code:
    var
      frmDesign: TForm;
      Timer1: TTimer;
      Label1: TLabel;

      Count: Integer; // This variable will be used for the counting!

    Procedure OnTimer(Sender: TObject);
    Begin
      Count := Count + 1;
      Label1.Caption := 'Form is allready open for: ' + IntToStr(Count) + ' seconds.'; // this is where it updates the labels text when the button is pressed.
    End;

    procedure InitForm;
    begin

      { The TForm; part }

      frmDesign := CreateForm;
      frmDesign.Left := 100;
      frmDesign.Top := 100;
      frmDesign.Width := 500;
      frmDesign.Height := 100;
      frmDesign.Caption := 'Test!';
      frmDesign.Color := ClWhite;
      frmDesign.Font.Color := ClBlack;
      frmDesign.Font.Name := 'Comic Sans MS';

      { The TButton; part }

      Timer1 := TTimer.Create(FrmDesign);
      Timer1.Interval := 1000;
      Timer1.Enabled := True;
      Timer1.OnTimer := @OnTimer;

      { The TLabel; Part }

      Label1 := TLabel.Create(FrmDesign);
      Label1.Parent := FrmDesign;
      Label1.Top := 10;
      Label1.Left := 10;
      Label1.Caption := 'Form is allready open for: 0 seconds.';
      Label1.Font.Size := 18;
      Label1.Font.Color := ClRed;
      Label1.Font.Name := 'Comic Sans MS';
    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.

    Advanced TTimer;

    under construction

    FAQ TTimer;

    no questions yet.

    End of TTimer;

  10. #10
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    Create a player setup form

    Designers stage

    Before you are going to make your form, you need to have an idea of what your form is going to look like. Make a concept in MSPaint. Why first make a concept? This is to make sure you don't forget anything. (once I forgot to add a TEdit; for the "loads till switch" option in the form and it takes a lot of time to add it in again once your form was done already).

    For this tutorial I made this concept, and this is what it should look like in the end:


    Working out your design

    Now it's time to start making the player form. This is going to the longest part of the tutorial. I'll number the steps were doing, so you can see where you where.

    In this tutorial I'm not going to post the complete code over and over again from what we got now. You should be able yourself to add the new code to your script. I will post the complete script at the end of the tutorial, so if you really can't find out what's going wrong, you can take a look at it. Or you could just post something here



    1. The first thing where going to do, is make the base, the TForm with his standard code. We will give the form a Height*Width of 800*600. You should not make your form much bigger because there are people who don't have a big screen. Yes this might be to large, but we will get rid of the left over space once the complete form design is done.
      SCAR Code:
      var
        frmDesign: TForm;

      procedure InitForm;
      begin
        frmDesign := CreateForm;
        frmDesign.Left := 100;
        frmDesign.Top := 100;
        frmDesign.Width := 800;
        frmDesign.Height := 600;
        frmDesign.Caption := 'Test!'; // Rename to whatever you want
        frmDesign.Color := ClWhite;
        frmDesign.Font.Color := ClBlack;
      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;

      Procedure ShowPlayerForm;
      begin
        SafeInitForm;
        SafeShowFormModal;
      end;

      {In your script you just have to call ShowPlayerForm;}

      Begin
        ShowPlayerForm;
      End.


    2. Now we are going to add items to our form. We will start of with the TListBox. Why not the banner first? That is because we don't know the width of our form yet, and so also not from the banner. Since we only have 1 TListBox, we don't have to put it in an array. So lets add it to our global variables, at the top of our script.
      SCAR Code:
      Var // and also your TForm will be in this variable list obviously
        PlayerList: TListBox;
      Now, to create the listbox in your form. We will set the .Top to 120, because we need to keep some space for banner, wish we will make later.
      SCAR Code:
      PlayerList := TListBox.Create(FrmDesign);
        PlayerList.Parent := FrmDesign;
        PlayerList.Top := 120;
        PlayerList.Left := 10;
        PlayerList.Width := 100;
        PlayerList.Height := 170;
        PlayerList.OnClick := @ItemClicked;
      When you have added that to your form, you'll notice your script wont work. That's true, because we don't have the ItemClicked procedure yet. We are going to use the same .OnClick procedure for every OnClick we will get.
      We do that as the following for our PlayerList:
      SCAR Code:
      Procedure ItemClicked(Sender: TObject);
      Begin
        Case Sender Of
          PlayerList: Begin
            // leave it empty for now
          End;
        End;
      End;
      Make sure you place the procedure above the form setup.

      When you click on the PlayerList in the form, nothing will happen, and the list is empty. We will leave it that way for now, because we first want to make all components are in the right place.


    3. The next thing we are going to add to our form, are the buttons. Since we have 7 buttons, we will put the TButton in an array, to keep our code short and easy over viewable.

      So we are going to add three variables to the global vars.
      • The button array (0 til 6)
      • An integer for the loop
      • A string array for the button captions

      That will look like the following:
      SCAR Code:
      Var
        Buttons: Array [0..6] Of TButton;
        I: Integer;
        CaptionA: Array Of String;
      Lets move on to the setup of the buttons in the form. We will use a For To Do loop, to do all the buttons. There will be 2 lines of buttons. The line at the right of the ListBox, and a line horizontal under the ListBox. We are not going to set the button position for every button, we will use a small formula instaid.
      SCAR Code:
      CaptionA := ['Load player list', 'Save player list', 'Add player',
        'Delete player', 'Start script', 'Exit without saving', 'Save and Exit'];
        For I := 0 To 6 Do // See*
        Begin
          Buttons[I] := TButton.Create(FrmDesign);
          Buttons[I].Parent := FrmDesign;
          Buttons[I].Height := 20;
          Buttons[I].OnClick := @ItemClicked;
          Buttons[I].Caption := CaptionA[I];
          Case I Of
            0..3: Begin // case 0, 1, 2, 3
              Buttons[I].Left := 120;
              Buttons[I].Top := 150 + I * 30;
              Buttons[I].Width := 100;
            End;
            4..6: Begin // case 4, 5, 6
              Buttons[I].Left := 10 + 160 * (I - 4); // See**
              Buttons[I].Top := 300;
              Buttons[I].Width := 150;
            End;
          End;
        End;
      * With this loop the script will first setup button[0], then button[1], ect, until button[6].
      ** Why the -4? else the .Left of Buttons[4] will be: 10 + 160 * 4, and then the button would be way to much to the right. So don't forget the -4


      The code above will take care of ALL your buttons. Take a very good look at the code above, and try to understand what the script exactly does.

      In the setup of the button we have called at all buttons @ItemClick; as onclick procedure. To see if all your buttons work you should replace your ItemClick procedure with this:
      SCAR Code:
      Procedure ItemClicked(Sender: TObject);
      Begin
        Case Sender Of
          PlayerList: Begin
            // keep empty for now
          End;
          Buttons[0]: Begin
            WriteLn('[BUTTON] Load players');
          End;
          Buttons[1]: Begin
            WriteLn('[BUTTON] Save players');
          End;
          Buttons[2]: Begin
            WriteLn('[BUTTON] Add player');
          End;
          Buttons[3]: Begin
            WriteLn('[BUTTON] Delete player');
          End;
          Buttons[4]: Begin
            WriteLn('[BUTTON] Start script');
          End;
          Buttons[5]: Begin
            WriteLn('[BUTTON] Stop without save');
          End;
          Buttons[6]: Begin
            WriteLn('[BUTTON] Stop but save first');
          End;
        End;
      End;
      When you run your script again, you will see that the debug says what you clicked when you click on a button.


    4. We are going to make 1 button working already. The "Exit without save" button. We need this button working because we are going to remove the "Frame" of the form (the frame where you can close the form, mini and maxi size it).

      Before we do that we are going to add a new variable to your script:
      SCAR Code:
      Var
        StartReady: Boolean;
      We need this variable to let the script know when the start button is pressed, cause if the start button is not pressed, but one of the exit buttons, the script should terminate after closing the form.

      Lets go to Procedure ShowPlayerForm; and change it to this:
      SCAR Code:
      Procedure ShowPlayerForm;
      begin
        SafeInitForm;
        SafeShowFormModal;
        If Not StartReady Then TerminateScript;
      end;
      This action is very important.

      Now, before removing the forms frame, we are going to make the "Exit without save" working. Go to your ItemClick procedure, and change Button[5] to This:
      SCAR Code:
      Buttons[5]: Begin
            frmDesign.ModalResult:= mrOk; // this will close the form
          End;
      When you run your form again, you'll see that the form closes when you press the button. We can remove the forms frame now, and you'll still be able to close the form.

      Add this to your form setup:
      SCAR Code:
      frmDesign.BorderStyle := bsNone;
      Now you successfully removed the frame. Now, since the background color of the form is white, it won't be good visible. So we will change the forms background color to red. This might be ugly, but later in the tutorial we will add a image as background, wish will give you form an amazing look.

      Change the background color of your form to red:
      SCAR Code:
      frmDesign.Color := ClRed;
      Now, we also know the height of our form already! we can shorten the height of your form so all that extra space won't be there anymore, since we are not going to use it. I can tell you the form height should be "330" now, don't make it any less, cause some of your buttons won't be visible anymore! (If you did it anyway you can close your form by pressing ALT+F4). So lets change the forms height to 330:
      SCAR Code:
      frmDesign.Height := 330;


    5. In this step we are going to add the labels to the form. The labels will tell the user what to put in the box next to it. We need to make 8 labels. So we will make an array from 0 to 7. Add this to your vars:
      SCAR Code:
      Var
        Labels: Array [0..7] Of TLabel;
      We will also need I and CaptionA again, but we don't have to declare them again in the global vars, since there already in there.

      Lets create the labels in your form:
      SCAR Code:
      CaptionA := ['Players name', 'Players pass', 'Players nick', 'Players Pin',
        'Loads till switch', 'Use this player?', 'SRL ID', 'SRL Pass'];
        For I := 0 To 7 Do // See*
        Begin
          Labels[I] := TLabel.Create(FrmDesign);
          Labels[I].Parent := FrmDesign;
          Labels[I].Caption := CaptionA[I];
          Case I Of
            0..5: Begin
              Labels[I].Top := 120 + 30 * I;
              Labels[I].Left := 230;
            End;
            6, 7: Begin
              Labels[I].Top := 180 + 30 * (I - 6); // See**
              Labels[I].Left := 450;
            End;
          End;
        End;
      * the loop will do Labels[1], then Labels[2], ect, until Labels[7]
      ** Again don't forget to do the -6, else it won't be at the correct position


      And actually that's all about the labels


    6. Then it's time to add the edit boxes! We will make 6 TEdits. Why not 8? cause for the "players.active" and the "players.loads" we are going to use a TComboBox. But we will start of making the TEdits now.

      So we are going to make 6 TEdit's, that will make it an array form 0 to 5:
      SCAR Code:
      Var
        Edits: Array [0..5] Of TEdit;
      Then, to create your editboxes in your form, add this to your form setup:
      SCAR Code:
      For I := 0 To 5 Do
        Begin
          Edits[I] := TEdit.Create(FrmDesign);
          Edits[I].Parent := FrmDesign;
          Edits[I].Width := 100;
          Edits[I].Height := 20;
          Case I Of
            0..3: Begin
              Edits[I].Left := 340;
              Edits[I].Top := 120 + 30 * I;
            End;
            4, 5: Begin
              Edits[I].Left := 540;
              Edits[I].Top := 180 + 30 * (I - 4); // See*
            End;
          End;
        End;
        Edits[1].PasswordChar := '*'; // See **
        Edits[3].PasswordChar := '*';
        Edits[5].PasswordChar := '*';
        Edits[2].MaxLength := 4; // See***
        Edits[3].MaxLength := 4;
      * Again, don't forget the -4
      ** with this other people can't see your password, except for "password" they will see "********"
      *** People's nickname should not be longer then 4, and there pin can be max 4 numbers


      Checkout the PasswordChar and MaxLength. Those are really cool tools to make your form more user friendly and better. We don't have to do anything more to those


    7. We can already see what the width of the form should be, but before we are going to edit that we will add the TComboBoxes first.

      2 empty spots in the form, so we are going to make an array form 0 to 1 in your global vars:
      SCAR Code:
      Var
        ComboBoxes: Array [0..1] Of TComboBox;
      And to create your ComboBoxes in the form, add this to your form setup:
      SCAR Code:
      For I := 0 To 1 Do
        Begin
          ComboBoxes[I] := TComboBox.Create(FrmDesign);
          ComboBoxes[I].Parent := FrmDesign;
          ComboBoxes[I].width := 100;
          ComboBoxes[I].Height := 20;
          ComboBoxes[I].Left := 340;
          ComboBoxes[I].Top := 240 + 30 * I;
        End;
        ComboBoxes[0].Items.Add('3'); // adding options to your combobox
        ComboBoxes[0].Items.Add('4');
        ComboBoxes[0].Items.Add('5');
        ComboBoxes[0].Items.Add('6');
        ComboBoxes[0].Items.Add('7');
        ComboBoxes[1].Items.Add('True');
        ComboBoxes[1].Items.Add('False');
      Adding items to your boxes takes some space, but it's not worth putting those in an array

      And that's already it for the combo boxes!


    8. Yesss, it's allmost time to get our banner, and to get rid of that ugly red background! But before we are going to do that we need to fix the width of your form.

      Edit this in your form setup:
      SCAR Code:
      frmDesign.Width := 650;
      Perfect! you now also know what the width and height of your banner and background should be!

      Banner: 630 * 100
      Background: 650 * 330


    9. In this step, we will give your form a very nice look! Before we are going to add any code, we need to have our images ready. One for the background, and one for the banner. I Made those 2 already for this tutorial, but feel free to make your own.

      Go to your scar folder, and create a new folder. We are going to call it "MKFormTut". Now, in your new map you'll have to save your 2 files. I'll call them "formbanner1.bmp" and "formbackground.bmp".

      formbackground.bmp
      formbanner1.bmp

      Make sure you save them in the correct folder. If your not sure where to exactly where to save them you can run this small script in a new scar:
      SCAR Code:
      Begin
        ClearDeBug;
        WriteLn(AppPath + 'MKFormTut\');
        WriteLn('');
      End.
      Now, we are going to add the images to the global vars. We have 2 images so an array from 0 to 1:
      SCAR Code:
      Var
        Images: Array [0..1] Of TImage;
      Before we start adding the images to the form, we have 3 new variables to add. b, w, h: integer;. Since we only need those 3 vars in the form setup, we will add the variables to the setup form procedure:
      SCAR Code:
      procedure InitForm;
      Var
        b, w, h: Integer;
      begin
        // ect
      Now it's time to add the images to the form setup. NOTE: the images must be declared between the TForm and the First other object (the TListBox in this case)! Here we go:
      SCAR Code:
      // TForm setup is here

        For I := 0 To 1 Do
        Begin
          Images[I] := TImage.Create(FrmDesign);
          Images[I].Parent := FrmDesign;
          Case I Of
            0: Begin
              Images[I].Width := 650;
              Images[I].Height := 330;
              b := loadbitmap(AppPath + 'MKFormTut\formbackground.bmp'); // See*
              getbitmapsize(b, w, h);
              copycanvas(getbitmapcanvas(b), Images[I].canvas, 0, 0, w,
              h, 0, 0, w, h);
            End;
            1: Begin
              Images[I].Width := 630;
              Images[I].Height := 100;
              Images[I].Left := 10;
              Images[I].Top := 10;
              b := loadbitmap(AppPath + 'MKFormTut\formbanner1.bmp'); // See*
              getbitmapsize(b, w, h);
              copycanvas(getbitmapcanvas(b), Images[I].canvas, 0, 0, w,
              h, 0, 0, w, h);
            End;
          End;
        End;

      // all other TObjects are here
      * If you gave it an other name, then set it to the other name of cause.

      ... ect

      Your form must look awesome now! If your not happy with your background, just change the bmp file and restart the script to see the new background/banner.


    10. Then, it's time to add our TTimer, because we don't want to bore people with the same banner all the time

      I made some more banners to add to our form:

      banner1
      banner2
      banner3

      Name them to whatever you want. But make sure you save them in your own folder. For this tutorial they need to come in your MKFormTut\ folder.

      First of this step, we are going to add the timer to your script. We will need the timer to change the banner images. Add the TTimer to your global vars:
      SCAR Code:
      Var
        BannerTimer: TTimer;
      And now, setup the timer in your form setup:
      SCAR Code:
      BannerTimer := TTimer.Create(FrmDesign);
        BannerTimer.Interval := 5000; // See*
        BannerTimer.Enabled := True; // See**
        BannerTimer.OnTimer := @ItemClicked; // See***
      * We want our banner to change every 5 seconds
      ** Activate your timer
      *** ItemClicked also works for your timer, so why not use it?


      Now we have our timer working, nothing is happening yet though. True, so lets change that! We will need a new global var so the script can remember wish banner it should display next:
      SCAR Code:
      Var
        CurBanner: Integer;
      We also need to add the, b, w and h again. This time we will add it tho the ItemClicked procedure:
      SCAR Code:
      Procedure ItemClicked(Sender: TObject);
      Var
        b, h, w: Integer;

      Then, lets add the BannerTimer, to the case in ItemClicked:
      SCAR Code:
      Case Sender Of
          BannerTimer: Begin
            Case CurBanner Of // See*
              0: b := loadbitmap(AppPath + 'MKFormTut\pcbanner.bmp'); // See**
              1: b := loadbitmap(AppPath + 'MKFormTut\srlbanner.bmp');
              2: b := loadbitmap(AppPath + 'MKFormTut\ownbanner.bmp');
              3: b := loadbitmap(AppPath + 'MKFormTut\formbanner.bmp');
            End;
            getbitmapsize(b, w, h);
            copycanvas(getbitmapcanvas(b), Images[1].canvas, 0, 0,
            w, h, 0, 0, w, h);
            CurBanner := CurBanner + 1; // See***
            If CurBanner > 3 Then CurBanner := 0; // See****
          End;
          // and here are your other clickitems
      * For B we need to load the current bitmap, to display as banner.
      ** Make sure your file is at the correct space, and change the name to what you called your banner
      *** To display the next banner next time, you need to add 1 to the current banner.
      **** The script can't display a banner that doesn't exists. So when you had your last banner we will go back to banner 0


      And there you go! Changing banners! Awesome!


      Congratz! You now have the base of an awesome looking form!

    11. You have completed the look of your form, and you have all the TComponents you need. Now it's time to make it work, and let the form actually do something!

      The first thing we are going to make work is the Add new player button. And with this step there comes a lot more then just the button's procedure. You see, all the players in your form, must be stored, in an array. An array wish holds all the info from the player setup boxes for all players created in the from

      We are going to make an array of TFormPlayer. TFormPlayer? is that correct? Yes, but it doesn't exist yet. Lets make a new type, and so create the TFormPlayer variable

      SCAR Code:
      Type
        TFormPlayer = Record
          Name, Pass, Nick, Pin, Loads, Active: String;
        End;

      So, for every box you have in your form, you add a string to your new Type. (or at least for what every player needs to have)

      Lets make a new variable: FP (form players) Add it to your global vars. We also want to add the variable "CurFP: Integer;". This one is there for the form to know what player it's showing at that moment.

      SCAR Code:
      CurFP: Integer;
        FP: Array Of TFormPlayer;

      Now, we have the stuff where we can store all players in your form. We will later convert it to Players[].Name, ect.

      We have made the new array for all of the players now. When we start the form, we want to have at least one player already. So we go to your ShowPlayerForm; procedure, and add the following to it:

      SCAR Code:
      Procedure ShowPlayerForm;
      begin
        SetArrayLength(FP, 1); // *
        SafeInitForm;
        SafeShowFormModal;
        If Not StartReady Then TerminateScript;
      end;
      * Now we have one player, so the form is ready to store the new player strings in that FP part.

      The FP part is done! Now it's time to make the AddPlayerButton work.
      When a users hits the add player button, we simply have to add a new part to the FP array. We do that by making the length of the array bigger.

      Jump to your ItemClicked procedure. There we have to work on our Add Player part. Within the add player case we are going to add this:

      SCAR Code:
      Buttons[2]: Begin
            SetArrayLength(FP, GetArrayLength(FP) + 1);
          End;

      * This will add a new player to your FP array, and gives you space for another form player.
      ** We need to let the form know, that we are going to edit the latest and newest player.


      Yay, now every time, you click the "add player button", you add a new player to your FP array! yaaaaaaaaaaaaaaaaaaay!
      But wait, nothing seems be happening? That's not true. We just need to visualize it. And that's where we are going to make the LoadPlayer(); procedure for. To finnish your "AddNewPlayer" button, we add this to your code:

      SCAR Code:
      Buttons[2]: Begin
            SetArrayLength(FP, GetArrayLength(FP) + 1);
            LoadFP(High(FP), True); // *
          End;

      * Yes, you have an error now, and if you want to fix that you better hurry to step 12!

    12. YES! It's time we actually make the big major procedure! Procedure LoadFP();! This is the place where all your player info gets loaded, and saved into the FP array. And most important of all, here will all your stuff get visualized!

      What this procedure is going to do is:
      1. Save your player info form your latest edit
      2. Load the players info of the player you want to edit now
      3. Show in the listbox all your players
      4. Show all the players data in the form for the current player (CurFP)

      This procedure is the key to everything in your code.

      So the first thing we are going to do, is create the new form procedure: (make sure you create it above the ItemClicked procedure)
      SCAR Code:
      Procedure LoadFP(WhatPlayer: Integer; SaveCurrentInfo: Boolean);
      Begin
      End;

      As you can see I have added 2 variables in the procedure. WhatPlayer; this is the integer the script need of wish player it has to load this time. SaveCurrentInfo; Does the script want to save the info of the player wish is currently displayed.

      When you have created this procedure you'll see that your script has no errors anymore And that makes it time to make the procedure actually do something.

      The first thing the scripts need to do, is to save the current player, before we change all the boxes, else it won't saved correctly, or just not saved at all. We start with the following code:

      SCAR Code:
      If SaveCurrentInfo Then
        Begin
          FP[CurFP].Name := Edits[0].Text;
          FP[CurFP].Pass := Edits[1].Text;
          FP[CurFP].Nick := Edits[2].Text;
          FP[CurFP].Pin := Edits[3].Text;
          FP[CurFP].Loads := ComboBoxes[0].Text;
          FP[CurFP].Active := ComboBoxes[1].Text;
        End;
      Now we are sure new player info has been saved and we can start loading an other player! The first thing I would like to do is giving the TListBox an update, so users can overview there players when changes are made. We do that by first clearing the box, and then loading the player list in again:

      SCAR Code:
      Var
        I: Integer;   // add this at the top of the procedure!

      // add this below the save player (in LoadFP)

        PlayerList.Clear;
        For I := 0 To High(FP) Do
        If (FP[I].Name = '') Then
          PlayerList.Items.Add(IntToStr(I) + ': ?')
        Else
          PlayerList.Items.Add(IntToStr(I) + ': ' + FP[I].Name);
      That will show a clear list of your players, numbered and players without a name yet will get a "?".

      The next step in LoadFP is that we need to load in the player we want to see now. We will fill the Edits and ComboBoxes up with the new CurFP:

      SCAR Code:
      CurFP := WhatPlayer;
        PlayerList.ItemIndex := CurFP; // *
        Edits[0].Text := FP[CurFP].Name;
        Edits[1].Text := FP[CurFP].Pass;
        Edits[2].Text := FP[CurFP].Nick;
        Edits[3].Text := FP[CurFP].Pin;
        ComboBoxes[0].Text := FP[CurFP].Loads;
        ComboBoxes[1].Text := FP[CurFP].Active;
      * Here we highlight the new CurFP in the listbox

      The last, but also very important thing, is to disable the "delete player" button, when there are less then 2 players. This is to prevent out of range errors, because you can't delete players if they don't exist. We will make a check like this:
      SCAR Code:
      Buttons[3].Enabled := (GetArrayLength(FP) > 1);
      That line might look a bit weird, but this is what it does: When there is more then 1 form player, it will enable the delete button, if there is only one, it will disable it.

      Congrats! LoadFP should be done for now! But you might have noticed, when you click the "add player" button you have 2 players? That's correct, because you already had one when you loaded the form, but you didn't see it yet. To fix this you'll have to add this at the bottom of your InitForm; procedure:
      SCAR Code:
      LoadFP(0, False);

    13. OnClick PlayerList

      When a user clicks on a char's name in the player list, the form should load in that player right after his name has been clicked. We can do this very very easily (thanks to LoadFP() <3 <3 <3).

      Go to your ItemClicked() procedure, there you find the onclick for PlayerList:. When a users clicks on a name, the PlayerList.ItemIndex will change to the array number of the clicked item in the list. Since the player names and positions in the player list perfectly match, we can just use that value for LoadFP:

      SCAR Code:
      PlayerList: Begin
            LoadFP(PlayerList.ItemIndex, True);
          End;
      And as you see above, the safe value is true, we don't want edited player info to get lost.

    14. [BUTTON] Delete player

      Omg I made a bullshit player and I want it out of my list. THAT'S POSSIBLE, with the delete button. This button has a very nasty peace of code, and it might be hard to understand, but I will try to explain it as good as I can.

      When you are going to delete a player, the FP array needs to get smaller. When you want to delete the last player of the list, it's simple. Just say HowMany FP = -1. But what if you want to delete player 3 of 5? Then there will be a gap in your array, so this is how it's done to prevent gap's like that:

      To remove player 3 of 5, you need to move player 4 to position 3, and player 5 to position 4. This will remove player 3 without making a gap. When you've moved all players after the player you want to delete, you can make the array smaller, with SetArrayLenght(-1).

      So in our delete player procedure we have 3 situations:

      situation 1. The player you want to delete is the last player of the list.
      situation 2. The player you want to delete is the last BUT 1 player of the list.
      situation 3. The player you want to delete has 2 or more charterers after it.

      We have to make the delete button work with all these 3 situations, and they will all work a little bit different!

      I'll show you the source of how it's done (very short but powerfull code):

      SCAR Code:
      Buttons[3]: Begin
            //WriteLn('[BUTTON] Delete player');
            If Not (CurFP = High(FP)) Then // *
            If (CurFP + 1 = High(FP)) Then
              FP[CurFP] := FP[High(FP)] // **
            Else
              For I := CurFP To High(FP) - 1 Do // ***
              FP[I] := FP[I + 1];
            SetArrayLength(FP, GetArrayLength(FP) - 1); // ****
            LoadFP(High(FP), False); // *****
          End;
      * When the case is situation 1, we don't have to peform any actions yet, we will just have to remove 1 player at the end of the procedure.
      ** When the case is situation 2, we have to switch the last player, to the "last but one" position! After that we can remove the last player at the end of the procedure.
      *** When the case is situation 3, we have to switch all players, after the CurFP, a place back in the array. After that we can remove the last player at the end of the procedure.
      **** This is where you say to your array, remove the last player of the array. This is fine to do now, since you have switched all your players in the array already.
      ***** After you have removed a player, your form has to load an other player and update the PlayerList! we do that with LoadFP() <3


      As I said above, the procedure might be very hard to understand. If you still don't get anything, feel free to PM me or make a post in this thread.

    15. The probably, most important button of the script! THE START BUTTON! MUAHAHAHA.

      AT THIS POINT ADD {.include srl\srl.scar} TO YOUR SCRIPT!

      You might have noticed, that all the players are now saved in FP[], all in strings. What we need to do now is to convert all strings into the SRL Players[] stoof! We have to convert the FP[].Name to Players[].Name, but also stuff like: FP[].Loads (STRING) to Players[].Integers[] (INTEGER). And that can't be done with some error checking, since you cannot convert the string "I would like to do 8 loads please" to the integer 8.

      When you click the start button, the script will loop trough all your players, checking for a correct setup. This will be very user and script friendly. For example: when you forgot to fill in your players nickname at player number 3, the script will load player 3 for you and say "oops, it seems like you forgot player 3 his nick!". To do this for all players and there subs like "name, nick, ect", will take some space of code, but, in trade for that it will result a very nice user friendly script.

      Before you start you have to save your current player. You just add "LoadFP(CurFP, True);" to the begin of your OnStartClick. we also need to set the length of your of players[] already. We do that like this:

      Note that the script does do nothing with the srl stats boxes, this is because the srl stats system is currently down, and I'll update it when it's back

      SCAR Code:
      Buttons[4]: Begin
            //WriteLn('[BUTTON] start script');
           
            LoadFP(CurFP, True);
            SetArrayLength(Players, GetArrayLength(FP));
           
            For I := 0 To High(FP) Do
            Begin

              SetArrayLength(Players[I].Integers, 1); // *
              SetArrayLength(Players[I].Strings, 0); // **
              SetArrayLength(Players[I].Booleans, 1); // ***
           
              If (FP[I].Name = '') Then
              Begin
                LoadFP(I, False);
                ShowMessage('Oops, you forgot to give player ' + IntToStr(I) + ' a name!');
                Exit;
              End Else
                Players[I].Name := FP[I].Name;
               
              If (FP[I].Pass = '') Then
              Begin
                LoadFP(I, False);
                ShowMessage('Oops, you forgot ' + FP[I].Name + ' his password!');
                Exit;
              End Else
                Players[I].Pass := FP[I].Pass;
               
              If (FP[I].Nick = '') Then
              Begin
                LoadFP(I, False);
                ShowMessage('Oops, you forgot ' + FP[I].Name + ' his nickname!');
                Exit;
              End Else
                Players[I].Nick := FP[I].Nick;
               
              Players[I].Pin := FP[I].Pin; // no need to check

              Try
                Players[I].Integers[0] := StrToInt(FP[I].Loads);
              Except
                LoadFP(I, False);
                ShowMessage('Oops, ' + FP[I].Name + ' has a wrong "loads" amoult!' +
                ' make sure it''s number only!');
                Exit;
              End;
             
              Try
                Players[I].Active := StrToBool(FP[I].Active);
              Except
                LoadFP(I, False);
                ShowMessage('Oops, ' + FP[I].Name + ' has a wrong "active" amoult!' +
                ' choose an option from the drop down box!');
                Exit;
              End;
           
            End;
           
            StartReady := true;
            frmDesign.ModalResult:= mrOk; // ****
          End;

      * Very important! here you must say how many custom integers your adding to your players[]
      ** Very important! here you must say how many custom strings your adding to your players[]
      *** Very important! here you must say how many custom booleans your adding to your players[]
      **** When the script gets at this line, it means that every player was succesfully checked, and that ALL your players are now in the old Players[CurrentPlayer]. stuff! READY TO ABUSE YOUR SCRIPT! With this like the form closes and continues the script!!!!!!!!!!!!!!!!!!! THE FORM IS WORKING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!

      CONGRATS!
      The script is working and it should now be working with your script!
      If you got a smooth result, feel free to post a screenshot and I will make sure it ends up in the "wall of fame"

    16. Download background and banner

    Last edited by MasterKill; 08-17-2009 at 06:31 AM.

  11. #11
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    Save & Load prayers into the form

    Under construction

  12. #12
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default


    Other components

    TRichEdit;

    TCheckBox;

    TProgressBar;

  13. #13
    Join Date
    Feb 2007
    Location
    South East England
    Posts
    2,906
    Mentioned
    2 Post(s)
    Quoted
    8 Post(s)

    Default

    What is this tutorial going to be on? I mean like for 12 posts? DARN!
    Jus' Lurkin'

  14. #14
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Torrent of Flame View Post
    What is this tutorial going to be on? I mean like for 12 posts? DARN!
    A huge form tutorial I hope I can learn people in the detail how to make a nice form. And this is prolly going to take me a week but i can and will finnish it

  15. #15
    Join Date
    Sep 2006
    Location
    include srl/srl.scar ( aussie)
    Posts
    2,875
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Nice, tho in the player section, a good thing to do is, with player loading is creating a new tab for each player... i use that and its good so you should add that, Because it would teach Tabs, and multiplayer.

  16. #16
    Join Date
    Dec 2007
    Posts
    2,766
    Mentioned
    2 Post(s)
    Quoted
    37 Post(s)

    Default

    Typo in post #11.

  17. #17
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by NiCbaZ View Post
    Nice, tho in the player section, a good thing to do is, with player loading is creating a new tab for each player... i use that and its good so you should add that, Because it would teach Tabs, and multiplayer.
    I use a TListBox for that You'll find out how to do that once the tutorial is done xD

    But thanks for you input

    Quote Originally Posted by Dr D. Dervish View Post
    Typo in post #11.
    LOL. care edited though

    oh and i have dyslexia

  18. #18
    Join Date
    May 2008
    Location
    127.0.0.1
    Posts
    705
    Mentioned
    1 Post(s)
    Quoted
    6 Post(s)

    Default

    would it be possible to have like check boxes like
    [x] Edgeville
    [ ] Varrock
    [ ] Falador
    <Wizzup> And he's a Christian
    <Wizzup> So he MUST be trusted
    ___________________________________________
    <Wizzup> she sounds like a dumb bitch

  19. #19
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Rubix View Post
    would it be possible to have like check boxes like
    [x] Edgeville
    [ ] Varrock
    [ ] Falador
    oh yes, I'll add that to the "Other components" post, and make a tutorial for it as soon as possible.

    SCAR Code:
    {name} :TCheckBox;

  20. #20
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    <3 MK your my hero

  21. #21
    Join Date
    Jul 2008
    Location
    Canada
    Posts
    1,612
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    When finished im gonna read this whole tutorial.

  22. #22
    Join Date
    May 2007
    Location
    Ohio
    Posts
    2,296
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Holy .... MK

  23. #23
    Join Date
    Feb 2007
    Location
    South East England
    Posts
    2,906
    Mentioned
    2 Post(s)
    Quoted
    8 Post(s)

    Default

    Where is this guys tut writer cup?!
    Jus' Lurkin'

  24. #24
    Join Date
    Mar 2008
    Location
    New Jersey
    Posts
    1,673
    Mentioned
    1 Post(s)
    Quoted
    9 Post(s)

    Default

    WOW, amazing tut. Will definitely finish reading all this, I scimmed through a lot. One thing to fix is I think here:

    SCAR Code:
    Timer1 := TTimer.Create(FrmDesign); // This will create your timer. Must be called.
      Timer1.Interval := 1000;            // After how many seconds must your procedure be called?
      Timer1.OnTimer := @OnTimer;         // Do what procedure if your timer is ready?
      Timer1.Enabled := True;             // Active timer? True for yes.

    I think you meant to put how many milliseconds after interval, not how many seconds. And under TTimer, in the first paragraph, you said "I'm going to learn you how" I think you meant either "I'm going to teach you how", or "I'm going to have you learn how". Probably the first one. But this tut is amazing, I never knew how to make a form, next script I make I am going to add a form to it and learn how from this tut. Good job, Rep+.

  25. #25
    Join Date
    May 2007
    Location
    Netherlands, Amersfoort
    Posts
    2,701
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Baked0420 View Post
    I think you meant to put how many milliseconds after interval, not how many seconds.
    oh oops! Bad thing when a tutorial is teaching something wrong Thanks for correcting me!



    And thanks all! I'm still working on it and I hope to finish it soon.

Page 1 of 6 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Complete Form Tutorial
    By BobboHobbo in forum OSR Advanced Scripting Tutorials
    Replies: 10
    Last Post: 05-29-2012, 01:36 PM
  2. The Form Tutorial
    By Dan Cardin in forum OSR Advanced Scripting Tutorials
    Replies: 28
    Last Post: 03-03-2011, 04:42 AM
  3. Newbie Form Tutorial
    By Da 0wner in forum OSR Advanced Scripting Tutorials
    Replies: 45
    Last Post: 12-15-2009, 05:48 AM
  4. Form TPopupMenu tutorial
    By Freddy1990 in forum OSR Advanced Scripting Tutorials
    Replies: 13
    Last Post: 12-14-2009, 07:25 PM
  5. Ultimate form tutorial
    By jhildy in forum OSR Advanced Scripting Tutorials
    Replies: 9
    Last Post: 02-21-2008, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •