Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

VB to Delphi

e_ce_c Member Posts: 3
In VB when you want to show another form, you write something like

Form2.Show or Form2.Load (depending on what you want).


How do you do these in Delphi? When I try to type

Tform2.

it finds an error.

Comments

  • zibadianzibadian Member Posts: 6,349
    : In VB when you want to show another form, you write something like
    :
    : Form2.Show or Form2.Load (depending on what you want).
    :
    :
    : How do you do these in Delphi? When I try to type
    :
    : Tform2.
    :
    : it finds an error.
    :
    Each form is contained in its own unit. First you need to add that unit name to your uses clause. Example: the Form1 (unit1) shows Form2 (unit2). In unit1 add the bolded code.
    [code]
    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
    Dialogs, [b]Unit2[/b];

    type
    TForm1 = class(TForm)
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;
    [/code]
    This allows you to call all the types, variables and routines specified between the "interface" and "implementation" from unit2.
    Next you should notice that all forms have a variable declared with them. This variable allows you to access the automatically created form. You should make your call using this variable, and not the type description, because the TForm1 doesn't exist in the memory.
    You can show a from in two ways: "modally" and "nonmodally". Or in other words "like a dialog" or "like another window". The methods for this are called ShowModal and Show, resp. Example:
    [code]
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    Form2.ShowModal; // Display it as a dialog.
    end;
    [/code]
    For more info on the ShowModal and Show methods see the help files. If you want to know how to create forms at runtime instead of design time, just ask, and I'll tell you.
  • e_ce_c Member Posts: 3
    Thanks a lot.If it's no trouble to you, tell me how to create forms at runtime.I would also appreciate it a lot if you could tell me where to find tutorials for Delphi,but not each link -> chapter.Thanks again :)
  • zibadianzibadian Member Posts: 6,349
    : Thanks a lot.If it's no trouble to you, tell me how to create forms at runtime.I would also appreciate it a lot if you could tell me where to find tutorials for Delphi,but not each link -> chapter.Thanks again :)
    :
    I don't know any tutorials on the web, because I never have searched for them. I would suggest the Delphi Developer's Guide bi Teixeira & Pacheco, and of course the help files.
    As for the runtime creation of a form. First you design the form normally. Then you need to remove the form from the "Auto-create forms" list in the program options. It is recommended that you also remove the global variable associated with your form.
    Depending on the task of the form, you can write a single function to handle the form's creation, showing and destruction (such as a message box); or you might need to do more work, in which case if could be helpful not to remove the global variable.
    Here is the code for a simple password form (mimicing the InputBox() function):
    [code]
    function CheckPassword(const APassword: string): boolean;
    var
    Form: TMyPasswordForm;
    begin
    Form := TMyPasswordForm.Create(Application); // Create the form
    with Form do try
    if ShowModal = mrOK then begin
    // If the user pressed OK then check the password
    Result := Edit1.Text = APassword;
    // Set the result to the case-sensitive comparison of the
    // entered password and the supplied password.
    end else
    // Else no password was entered, and thus automatically failed
    Result := false;
    finally
    Form.Free;
    // Destroy the form again
    end;
    end;
    [/code]
    This example shows how to create a dialog box at runtime and free it again after it has been used. It also shows the usage of the "with ... do" statement, and a "try...finally" block. Both are well described in the help files.
    Important points to remember:
    - Normal forms are always owned by the Application, while MDI child forms are always owned by the MDI frame.
    - You are responsible for the memory management of the forms (ie. you need to call Free), unless the form is still active when the program terminates. This often means using a try...finally block to make sure that the form is freed even after an exception.
    - After the form has been freed you loose all the values in the controls on that form (in the example the entered password).

    I think that's about it. If you have any more questions feel free to ask.
Sign In or Register to comment.