RAD Studio |  |

Passing Additional Arguments to Forms
See Also
Collapse All

Typically, you create forms for your application from within the IDE. When created this way, the forms have a constructor that takes one argument, Owner, which is the owner of the form being created. (The owner is the calling application object or form object.) Owner can be nil.

To pass additional arguments to a form, create a separate constructor and instantiate the form using this new constructor. The example form class below shows an additional constructor, with the extra argument whichButton. This new constructor is added to the form class manually.

Copy Code

    TResultsForm = class(TForm)
      ResultsLabel: TLabel;
      OKButton: TButton;
      procedure OKButtonClick(Sender: TObject);
    private
    public
      constructor CreateWithButton(whichButton: Integer; Owner: TComponent);
    end;

Copy Code

    class TResultsForm : public TForm
    {
    __published:    // IDE-managed Components
    TLabel *ResultsLabel;
    TButton *OKButton;
    void __fastcall OKButtonClick(TObject *Sender);
    private:        // User declarations
    public:         // User declarations
    virtual __fastcall TResultsForm(TComponent* Owner);
       virtual __fastcall TResultsForm(int whichButton, TComponent* Owner);
    };

Here's the manually coded constructor that passes the additional argument, whichButton. This constructor uses the whichButton parameter to set the Caption property of a Label control on the form.

Copy Code

    constructor CreateWithButton(whichButton: Integer; Owner: TComponent);
    begin
      inherited Create(Owner);
      case whichButton of
        1: ResultsLabel.Caption := "You picked the first button.";
        2: ResultsLabel.Caption := "You picked the second button.";
        3: ResultsLabel.Caption := "You picked the third button.";
      end;
    end;

Copy Code

    void__fastcall TResultsForm::TResultsForm(int whichButton, TComponent* Owner)
    : TForm(Owner)
    {
    switch (whichButton) {
    case 1:
    ResultsLabel->Caption = "You picked the first button!";
    break;
    case 2:
    ResultsLabel->Caption = "You picked the second button!";
    break;
    case 3:
    ResultsLabel->Caption = "You picked the third button!";
    }
    }

When creating an instance of a form with multiple constructors, you can select the constructor that best suits your purpose. For example, the following OnClick handler for a button on a form calls creates an instance of TResultsForm that uses the extra parameter:

Copy Code

    procedure TMainForm.SecondButtonClick(Sender: TObject);
    var
      rf: TResultsForm;
    begin
      rf := TResultsForm.CreateWithButton(2, self);
      rf.ShowModal;
      rf.Free;
    end;

Copy Code

    void __fastcall TMainMForm::SecondButtonClick(TObject *Sender)
    {
    TResultsForm *rf = new TResultsForm(2, this);
    rf->ShowModal();
    delete rf;
    }

See Also

TForm

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!