Form2 引用Form1中的函数

在delphi 设计GUI的过程中,窗体通信是不可缺少的一部分。若想解决Form2调用Form1函数;本文是通过如下步骤实现:

  1. 在Form1中建立公共函数,在var 下建立 procedure form2_form1(i:Integer);
  2. 在函数form2_form1(i:Integer)中调用Form1组件时 Form1.Edit1.text:='GHG';
  3. 在Form2 中implementation 下写入uses Unit1;在Form1 中implementation 下写入uses Unit2;
  4. 在Form2中调用form2_form1,而后运行。

框图:

Form1的代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public

    { Public declarations }
  end;

var
  Form1: TForm1;
  procedure form2_form1(i:Integer);//定义的公共函数
implementation
uses Unit2;
procedure form2_form1(i:Integer);
begin
 Form1.Edit1.text:='GHG';//form2调运Form1的函数,如需要用到form1的组件,应注意在组件前添fORM1
end;
{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
     Form2.Show;
end;

end.

 Form2的代码:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation
uses Unit1;
{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  i:Integer;
begin
   i:=5;
   Edit1.Text:='2';
   form2_form1(i);
    
end;

end.

运行结果:

猜你喜欢

转载自blog.csdn.net/weixin_42528089/article/details/84550469
今日推荐