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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top