Passing information from one form to another?
Asked Answered
S

1

6

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?

Suwannee answered 22/2, 2014 at 19:2 Comment(1)
By adding methods and passing arguments? :)Fm
T
6

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.

Terrorism answered 22/2, 2014 at 21:33 Comment(1)
Your answer is much appreciated!Suwannee

© 2022 - 2024 — McMap. All rights reserved.