Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data between forms in Delphi?

Tags:

forms

delphi

It may seem a little newbie, but I really have got problem with it. I have a form (not the main one)for getting many different data from the user and I want to pass it to a manager class for creating an object with these. The problem is that I can't have this class to use the other unit (getting circle uses) and also it doesn't have access to the manager class instance (which is in main form).

So, what shall I do? I've already considered using public variable but I have a bad feeling about it (regarding OOD patterns).

like image 999
Mahm00d Avatar asked Jun 02 '10 08:06

Mahm00d


People also ask

How do you get a variable from another form in Delphi?

In such case, all you need to do in Unit2 is to add Unit1 to its 'uses list' (press Alt+F11, or use File/'Use Unit...', while in Unit2 or while editing Form2 ). Then you can use Form1. idList to access the variable anywhere inside Unit2 . ( Form1 is the global instance variable of TForm1 in Unit1 ).

How do I change the main form in Delphi?

To assign a different form to the MainForm property, select the form on the Project > Options > Forms dialog box at design time. MainForm cannot be modified at run time (it is read-only at run time). Note: By default, the form created by the first call to CreateForm in a project becomes the application's main form.


2 Answers

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.

like image 144
Wodzu Avatar answered Oct 22 '22 22:10

Wodzu


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.

like image 4
Azarien Avatar answered Oct 22 '22 21:10

Azarien