How to pass data between forms in Delphi?

My suggestion is to decouple data from the GUI because this is causing your problem. If you have a form which gathers data from the user then you should distinguish the data from the form(TForm).

For example, let's assume that you have some instance of TForm and a form, which is built from three fields: username, age and location. You want the user to enter those three things, but when the user closes the form, you should pass this inserted data onto some object. Form closes, it is freed, but the object persist. Then you pass this object to your manager object.

Simple example:

This is your record which will hold the data

type
  TGatheredData = record
    Name: String[40];
    Age: Byte;
    Location: String[40];
end;

Your TForm1 might have an aditional constructor:

constructor TForm1.Create(AOwner: TComponent; var GatheredData: TGatheredData );
begin
  inherited Create(AOwner);
  FGatheredData := GatheredData;
  //you may want to deserialize GatheredData here and show the data in your form controls
end;

You call it, pass GatheredData and then your are showing your form.

Next, when closing form, you pick upd the data from the form controls.

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Self.ModalResult = mrOk then
  begin
    //serialize your object
    FGatheredData.Name := '';//name taken from control f.e. TEdit
    FGatheredData.Age := '';//name taken from control f.e. TSpinButton
    FGatheredData.Location := '';//name taken from control f.e. TEdit
  end;
end;

Having this record of data, you may now pass it in the same manner to your Manager object. You decoupled data from GUI in this way, and you may easly plugin in your record to a number of different forms.

Just remember to declare your record type in external unit and use that unit in your manager unit and forms unit.

Hope this helps a little.


The "manager class" shouldn't be in either form's unit, but in its own. By separating GUI code from bussiness logic you avoid problems such like this.


[Edit: I originally put this answer in a comment, but decided to move it out into full answer. TDatamodules are too important and too common in Delphi not to emphasize them and they provide built-in easy-to-use means of seperating gui from logic and data.]

Other people have given good comments about decoupling gui from the logic and data. Surprisingly, I don't think anybody has mentioned that in Delphi TDatamodules are one main means of doing this. You put your data and logic on the Datamodule, then have both forms "use" the Datamodule to get access to its data and methods. Here is brief intro: http://delphi.about.com/od/database/l/aa101601a.htm

Both of your forms (and other forms) can access datasets or other data/datastructures that are located on/in a Datamodule unit. There should be an easy to find sample project out there illustrating the setup, since it is (or at least was) the standard way to construct Delphi apps.

Tags:

Forms

Delphi