Form1 如何向Form2传递数据

 

  1. 首先form1有一个全局变量或者函数是在public下
  2. 然后form2的uses下引用form的单元名
  3. 这个时候就可以了

1.设置框图

Form1 如何向Form2传递数据_第1张图片

2.code 

2.1 form1的代码:

unit Unit1;

interface

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

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

  end;

var
  Form1: TForm1;
  ss:String;//全局变量
implementation

uses Unit2;//定义了Form2的单元

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
    Form2.Show;
    ss:=RichEdit1.Text;//赋值
    Edit1.Text:=ss;
end;

end.

2.2 Form2的代码:

unit Unit2;

interface

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

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

var
  Form2: TForm2;

implementation
uses Unit1;
{$R *.dfm}

procedure TForm2.BitBtn1Click(Sender: TObject);
begin
       Edit1.Text:=ss;

end;

end.

 3.运行结果:

Form1 如何向Form2传递数据_第2张图片

4.结论

  完成了数据从Form1 到Form2的传递。 

你可能感兴趣的:(Delphi,全局变量)