Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing information from one form to another?

How would you pass information from one form to another form, such as value entered in an edit on form 1 and when a button is pressed the information in the edit will be sent to a label on form 2.

How can I do this?

like image 387
Grass isGood Avatar asked Feb 22 '14 19:02

Grass isGood


1 Answers

If it's simply passing the content of an edit control on one form to a label on another form, you just set the Label.Caption (components on a form are published properties of that form).

Presuming that you've added the unit containing TForm2 to the TForm1 unit and created both forms, and that they both have their default names:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Label1.Caption := Edit1.Text;
end;

If you haven't already created the second form, you can do so and assign the label caption at the same time. This example shows how to create a new form, set the label caption, show the form and wait for the user to close it, and then free the form:

procedure TForm1.Button1Click(Sender: TObject);
var
  NewForm: TForm2;
begin
  NewForm := TForm2.Create(nil);
  try
    NewForm.Label1.Caption := Edit1.Text;
    NewForm.ShowModal;
  finally
    NewForm.Free;
  end;
end;

For more complex needs, you can create properties to set or methods on the second form that you can call, passing information as parameters to the procedure.

like image 126
Ken White Avatar answered Nov 14 '22 22:11

Ken White