How to Create Forms! - Mad Cow

Discussion in 'Code Snippets and Tutorials' started by Anonymous, Jan 17, 2007.

  1. Anonymous

    Anonymous Guest

    Creating forms and applying them to scripts.
    By: Mad Cow


    What is a Form?
    A form is basically an easy way to set-up your scripts. It provides a user-friendly interface where you can type in all your information.

    Requirements
    Some basic SCAR knowledge required.

    Overview
    We will be making a Form that saves our Username and Password. You will learn how to create: Labels, Edit boxes and Buttons.

    Getting started
    What we will need to do is open the Form editor in SCAR. You can do this by going Tools -> Form Editor. Once that is open, 3 boxes should appear.

    Labels
    Now on the top box, click on the "A" then click anywhere on the Blank form(preferably towards the left). Now we must change the name.

    Eg.[​IMG]


    Now create another label for the password.

    Edit boxes
    Now that we have created the labels, we need some boxes for the user to add text in.
    Click on the box next to the Label one and make a click next to the Username and Password box. You may remove the caption.

    Eg.[​IMG]

    Start Button
    Now we got the Hard stuff out the way it's time to add a button so when you click, the Form will close. A couple of spaces to the right of the "Edit" button should be a box with the word "OK" in it. Click on that then click where you want your button to be. I"d personally put it the start button at the bottom, but it"s your choice.

    Eg.[​IMG]

    Resizing and changing the name
    Resize the window to whatever fits best.

    Now click on the icon that looks like this:
    [​IMG]

    Now click click anywhere on the for(Not on the buttons or labels) then change the Caption.

    Eg.[​IMG]

    Implementing the form into the script
    Save it by pressing the save icon then close the form editor.

    Now we go to:
    Tools -> Load DFM form

    Then click on the form that you just saved. You should get a bunch of lines. Add the files that it says to the variables.

    Eg.
    Code (Text):
    1. var
    2.   frmDesign : TForm;
    3.   Label1 : TLabel;
    4.   Label2 : TLabel;
    5.   Edit2 : TEdit;
    6.   Edit1 : TEdit;
    7.   Button1 : TButton;
    Now create a new procedure called initForm then paste all those lines above the var into it.

    Eg.
    Code (Text):
    1. program FormBegginerTutorial;
    2. var
    3.   frmDesign : TForm;
    4.   Label1 : TLabel;
    5.   Label2 : TLabel;
    6.   Edit2 : TEdit;
    7.   Edit1 : TEdit;
    8.   Button1 : TButton;
    9.  
    10. procedure InitForm;
    11. begin
    12.   frmDesign := CreateForm;
    13.   frmDesign.Left := 250;
    14.   frmDesign.Top := 114;
    15.   frmDesign.Width := 263;
    16.   frmDesign.Height := 180;
    17.   frmDesign.Caption := 'User and Pass Saver';
    18.   frmDesign.Color := clBtnFace;
    19.   frmDesign.Font.Color := clWindowText;
    20.   frmDesign.Font.Height := -11;
    21.   frmDesign.Font.Name := 'MS Sans Serif';
    22.   frmDesign.Font.Style := [];
    23.   frmDesign.Visible := True;
    24.   frmDesign.PixelsPerInch := 96;
    25.   Label1 := TLabel.Create(frmDesign);
    26.   Label1.Parent := frmDesign;
    27.   Label1.Left := 32;
    28.   Label1.Top := 40;
    29.   Label1.Width := 51;
    30.   Label1.Height := 13;
    31.   Label1.Caption := 'Username:';
    32.   Label2 := TLabel.Create(frmDesign);
    33.   Label2.Parent := frmDesign;
    34.   Label2.Left := 33;
    35.   Label2.Top := 65;
    36.   Label2.Width := 49;
    37.   Label2.Height := 13;
    38.   Label2.Caption := 'Password:';
    39.   Edit2 := TEdit.Create(frmDesign);
    40.   Edit2.Parent := frmDesign;
    41.   Edit2.Left := 93;
    42.   Edit2.Top := 37;
    43.   Edit2.Width := 121;
    44.   Edit2.Height := 21;
    45.   Edit2.TabOrder := 8;
    46.   Edit1 := TEdit.Create(frmDesign);
    47.   Edit1.Parent := frmDesign;
    48.   Edit1.Left := 93;
    49.   Edit1.Top := 62;
    50.   Edit1.Width := 121;
    51.   Edit1.Height := 21;
    52.   Edit1.TabOrder := 9;
    53.   Button1 := TButton.Create(frmDesign);
    54.   Button1.Parent := frmDesign;
    55.   Button1.Left := 79;
    56.   Button1.Top := 99;
    57.   Button1.Width := 75;
    58.   Button1.Height := 25;
    59.   Button1.Caption := 'Start!';
    60.   Button1.TabOrder := 10;
    61. end;
    62.  
    63. begin
    64. end.
    Now we will add three more procedures.

    Code (Text):
    1. procedure SafeInitForm;
    2. var
    3.   v: TVariantArray;
    4. begin
    5.   setarraylength(V, 0);
    6.   ThreadSafeCall('InitForm', v);
    7. end;
    8.  
    9. procedure ShowFormModal;
    10. begin
    11.   frmDesign.ShowModal;
    12. end;
    13.  
    14. procedure SafeShowFormModal;
    15. var
    16.   v: TVariantArray;
    17. begin
    18.   setarraylength(V, 0);
    19.   ThreadSafeCall('ShowFormModal', v);
    20. end;
    These procedures are just to make sure that the form will not freeze.

    Add these two lines to your main begin.
    Code (Text):
    1. begin
    2.   SafeInitForm;
    3.   SafeShowFormModal;
    4. end.
    Now we will need to remove the form visibility.
    Find the line that looks like this.
    Code (Text):
    1. frmDesign.Visible := True;
    And change it to this
    Code (Text):
    1. frmDesign.Visible := False;
    Now we are basically done.

    In the InitForm procedure, look for the the button lines.
    You should find something like this
    Code (Text):
    1.   Button1 := TButton.Create(frmDesign);
    2.   Button1.Parent := frmDesign;
    3.   Button1.Left := 79;
    4.   Button1.Top := 99;
    5.   Button1.Width := 75;
    6.   Button1.Height := 25;
    7.   Button1.Caption := 'Start!';
    8.   Button1.TabOrder := 10;
    Change it to
    Code (Text):
    1.   Button1 := TButton.Create(frmDesign);
    2.   Button1.Parent := frmDesign;
    3.   Button1.OnClick := @ButtonClick;
    4.   Button1.Left := 79;
    5.   Button1.Top := 99;
    6.   Button1.Width := 75;
    7.   Button1.Height := 25;
    8.   Button1.Caption := 'Start!';
    9.   Button1.TabOrder := 10;
    This will call the procedure ButtonClick when clicked.

    Making the start button useful
    Lets create a new procedure above InitForm.
    Code (Text):
    1. procedure ButtonClick(sender: TObject);
    2. begin
    3. end;
    Now lets make the button close when we click it. Add the following lines of scar to your buttonclick procedure.
    Code (Text):
    1.   WriteLn('You have clicked the button');
    2.   frmDesign.Caption:= frmDesign.Caption + '.';
    3.   frmDesign.ModalResult:= mrOk;
    You should now have something like this:
    Code (Text):
    1. program FormBegginerTutorial;
    2. var
    3.   frmDesign : TForm;
    4.   Label1 : TLabel;
    5.   Label2 : TLabel;
    6.   Edit2 : TEdit;
    7.   Edit1 : TEdit;
    8.   Button1 : TButton;
    9.  
    10. procedure ButtonClick(sender: TObject);
    11. begin
    12.   WriteLn('You have clicked the button');
    13.   frmDesign.Caption:= frmDesign.Caption + '.';
    14.   frmDesign.ModalResult:= mrOk;
    15. end;
    16.  
    17.  
    18. procedure InitForm;
    19. begin
    20.   frmDesign := CreateForm;
    21.   frmDesign.Left := 250;
    22.   frmDesign.Top := 114;
    23.   frmDesign.Width := 263;
    24.   frmDesign.Height := 180;
    25.   frmDesign.Caption := 'User and Pass Saver';
    26.   frmDesign.Color := clBtnFace;
    27.   frmDesign.Font.Color := clWindowText;
    28.   frmDesign.Font.Height := -11;
    29.   frmDesign.Font.Name := 'MS Sans Serif';
    30.   frmDesign.Font.Style := [];
    31.   frmDesign.Visible := False;
    32.   frmDesign.PixelsPerInch := 96;
    33.   Label1 := TLabel.Create(frmDesign);
    34.   Label1.Parent := frmDesign;
    35.   Label1.Left := 32;
    36.   Label1.Top := 40;
    37.   Label1.Width := 51;
    38.   Label1.Height := 13;
    39.   Label1.Caption := 'Username:';
    40.   Label2 := TLabel.Create(frmDesign);
    41.   Label2.Parent := frmDesign;
    42.   Label2.Left := 33;
    43.   Label2.Top := 65;
    44.   Label2.Width := 49;
    45.   Label2.Height := 13;
    46.   Label2.Caption := 'Password:';
    47.   Edit2 := TEdit.Create(frmDesign);
    48.   Edit2.Parent := frmDesign;
    49.   Edit2.Left := 93;
    50.   Edit2.Top := 37;
    51.   Edit2.Width := 121;
    52.   Edit2.Height := 21;
    53.   Edit2.TabOrder := 8;
    54.   Edit1 := TEdit.Create(frmDesign);
    55.   Edit1.Parent := frmDesign;
    56.   Edit1.Left := 93;
    57.   Edit1.Top := 62;
    58.   Edit1.Width := 121;
    59.   Edit1.Height := 21;
    60.   Edit1.TabOrder := 9;
    61.   Button1 := TButton.Create(frmDesign);
    62.   Button1.OnClick := @ButtonClick;
    63.   Button1.Parent := frmDesign;
    64.   Button1.Left := 79;
    65.   Button1.Top := 99;
    66.   Button1.Width := 75;
    67.   Button1.Height := 25;
    68.   Button1.Caption := 'Start!';
    69.   Button1.TabOrder := 10;
    70. end;
    71.  
    72. procedure SafeInitForm;
    73. var
    74.   v: TVariantArray;
    75. begin
    76.   setarraylength(V, 0);
    77.   ThreadSafeCall('InitForm', v);
    78. end;
    79.  
    80. procedure ShowFormModal;
    81. begin
    82.   frmDesign.ShowModal;
    83. end;
    84.  
    85. procedure SafeShowFormModal;
    86. var
    87.   v: TVariantArray;
    88. begin
    89.   setarraylength(V, 0);
    90.   ThreadSafeCall('ShowFormModal', v);
    91. end;
    92.  
    93. begin
    94.   SafeInitForm;
    95.   SafeShowFormModal;
    96. end.

    Almost done! Now we will add 2 new variables called Username and Password and save them as strings.



    Code (Text):
    1. var
    2.   Username, Password : String;
    Now create another procedure called SetupAll.
    We will now add the following lines of scar.

    Code (Text):
    1. procedure SetupAll;
    2. begin
    3.   Username := Edit1.Text;
    4.   Password := Edit2.Text;
    5. end;
    Now what this will do is save whatever is in the Edit boxes as Username and Password.

    Just to test that it works, in the main procedure add:
    Code (Text):
    1.   WriteLn('Username = ' + Username);
    2.   Writeln('Password = ' + Password);
    And now you should successfully have your Username and password you typed in.

    NOTE: To make your password appear as "*" when you type, add the following scar.

    Code (Text):
    1.   Edit2 := TEdit.Create(frmDesign);
    2.   Edit2.Parent := frmDesign;
    3.   Edit2.Left := 93;
    4.   Edit2.Top := 62;
    5.   Edit2.Width := 121;
    6.   Edit2.Height := 21;
    7.   Edit2.TabOrder := 9;
    Change it to

    Code (Text):
    1.   Edit2 := TEdit.Create(frmDesign);
    2.   Edit2.Parent := frmDesign;
    3.   Edit2.Left := 93;
    4.   Edit2.Top := 62;
    5.   Edit2.Width := 121;
    6.   Edit2.Height := 21;
    7.   Edit2.TabOrder := 9
    8.   Edit2.PasswordChar:= '*';
    I finished with the following scar:
    Code (Text):
    1. program FormBegginerTutorial;
    2. var
    3.   frmDesign : TForm;
    4.   Label1 : TLabel;
    5.   Label2 : TLabel;
    6.   Edit2 : TEdit;
    7.   Edit1 : TEdit;
    8.   Button1 : TButton;
    9.   Username, Password : String;
    10.  
    11. procedure ButtonClick(sender: TObject);
    12. begin
    13.   WriteLn('You have clicked the button');
    14.   frmDesign.Caption:= frmDesign.Caption + '.';
    15.   frmDesign.ModalResult:= mrOk;
    16. end;
    17.  
    18.  
    19. procedure InitForm;
    20. begin
    21.   frmDesign := CreateForm;
    22.   frmDesign.Left := 250;
    23.   frmDesign.Top := 114;
    24.   frmDesign.Width := 263;
    25.   frmDesign.Height := 180;
    26.   frmDesign.Caption := 'User and Pass Saver';
    27.   frmDesign.Color := clBtnFace;
    28.   frmDesign.Font.Color := clWindowText;
    29.   frmDesign.Font.Height := -11;
    30.   frmDesign.Font.Name := 'MS Sans Serif';
    31.   frmDesign.Font.Style := [];
    32.   frmDesign.Visible := False;
    33.   frmDesign.PixelsPerInch := 96;
    34.   Label1 := TLabel.Create(frmDesign);
    35.   Label1.Parent := frmDesign;
    36.   Label1.Left := 32;
    37.   Label1.Top := 40;
    38.   Label1.Width := 51;
    39.   Label1.Height := 13;
    40.   Label1.Caption := 'Username:';
    41.   Label2 := TLabel.Create(frmDesign);
    42.   Label2.Parent := frmDesign;
    43.   Label2.Left := 33;
    44.   Label2.Top := 65;
    45.   Label2.Width := 49;
    46.   Label2.Height := 13;
    47.   Label2.Caption := 'Password:';
    48.   Edit1 := TEdit.Create(frmDesign);
    49.   Edit1.Parent := frmDesign;
    50.   Edit1.Left := 93;
    51.   Edit1.Top := 37;
    52.   Edit1.Width := 121;
    53.   Edit1.Height := 21;
    54.   Edit1.TabOrder := 8;
    55.   Edit2 := TEdit.Create(frmDesign);
    56.   Edit2.Parent := frmDesign;
    57.   Edit2.Left := 93;
    58.   Edit2.Top := 62;
    59.   Edit2.Width := 121;
    60.   Edit2.Height := 21;
    61.   Edit2.TabOrder := 9;
    62.   Edit2.PasswordChar:= '*';
    63.   Button1 := TButton.Create(frmDesign);
    64.   Button1.OnClick := @ButtonClick;
    65.   Button1.Parent := frmDesign;
    66.   Button1.Left := 79;
    67.   Button1.Top := 99;
    68.   Button1.Width := 75;
    69.   Button1.Height := 25;
    70.   Button1.Caption := 'Start!';
    71.   Button1.TabOrder := 10;
    72. end;
    73.  
    74. procedure SafeInitForm;
    75. var
    76.   v: TVariantArray;
    77. begin
    78.   setarraylength(V, 0);
    79.   ThreadSafeCall('InitForm', v);
    80. end;
    81.  
    82. procedure ShowFormModal;
    83. begin
    84.   frmDesign.ShowModal;
    85. end;
    86.  
    87. procedure SafeShowFormModal;
    88. var
    89.   v: TVariantArray;
    90. begin
    91.   setarraylength(V, 0);
    92.   ThreadSafeCall('ShowFormModal', v);
    93. end;
    94.  
    95. procedure SetupAll;
    96. begin
    97.   Username := Edit1.Text;
    98.   Password := Edit2.Text;
    99. end;
    100.  
    101. begin
    102.   SafeInitForm;
    103.   SafeShowFormModal;
    104.   SetupAll;
    105.   WriteLn('Username = ' + Username);
    106.   Writeln('Password = ' + Password);
    107. end.
    Congratulations! You just made a form!!

    Deathader: All programmers can look in the programmer forum to learn how to make a form have AUTH capeabilities ;)
     
  2. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    wow thanks for this
    i always wandered how to make it look like a program
    thanks again deathader
     
  3. test123

    test123 Level IV

    Joined:
    Nov 3, 2006
    Messages:
    935
    Likes Received:
    0
    Location:
    Australia
    hey

    hey deathader i was wondering if its possible to make a set of instructions/code invisible when the script has been executed...for example:

    I wanted my script to open a webpage but when the script was playing it wouldn't show the webpage being opened..
     
  4. sean425

    sean425 Level II

    Joined:
    Jan 12, 2007
    Messages:
    163
    Likes Received:
    0
    Very nice tutorial death +rep im using this in my user shop aber script, ill give credit :D
     
  5. Anonymous

    Anonymous Guest

    Re: hey

    Sorry. SCAR is a macro. It can only control stuff if the page IS open.
     
  6. softball

    softball Level III

    Joined:
    Dec 26, 2006
    Messages:
    698
    Likes Received:
    0
    Location:
    Your Sock!
    wow that looks as if it took a lot of hard work good job!
     
  7. Anonymous

    Anonymous Guest

    This wasn't made by me. It was made by my buddy MadCow. He's a SRL developer :)