Call procedures from different forms

2020-04-21 01:48发布

问题:

I am using Lazarus and I have a form called TForm1 and the unit name's Unit 1. Inside here I have a procedure called mergeDATfile(a:shortint); that makes some stuff.

By the way I had to create another form called TForm2 and inside this I have button (Button1). When it is pressed, it must call mergeDATfile(a:shortint); from the 1st form.

How could I do it?

回答1:

The obvious solution is to move the MergeDatFile function into a common unit which can then be used by both form units.



回答2:

I am assuming that the method is a method of TForm1 because it operates on the members of TForm1. In which case you would do the following:

  1. Expose the method as a public method.
  2. Arrange for the TForm2 instance to have access to an instance of TForm1.
  3. Call the method on the TForm1 instance.

If my assumption is incorrect then the method should be moved out of TForm1 into another unit that can be used by both of your forms.