1. 程式人生 > >Form1 如何向Form2傳遞資料

Form1 如何向Form2傳遞資料

 

  1. 首先form1有一個全域性變數或者函式是在public下
  2. 然後form2的uses下引用form的單元名
  3. 這個時候就可以了

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.執行結果:

4.結論

  完成了資料從Form1 到Form2的傳遞。