unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  ffff:integer;                     //全局变量
implementation
 uses
    unit2;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin
     ffff:=1;
     unit2.Form2.ShowModal;
end;
end.以下是unit2代码:
unit Unit2;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm2 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form2: TForm2;
implementation
  uses
  unit1;
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
begin
  if unit1.ffff=0 then
  begin
     edit1.Text:='0';
     end
     else if unit1.ffff=1 then
     begin
     edit1.Text:='1';
     end;
end;
end.问题是:form2的edit1总是显示0,而不是1(就是初始值).
就算在unit1中初始化了ffff,点击了form1的按钮之后,传到form2的ffff值还是初始值。

解决方案 »

  1.   

    写到BUTTON1下面就可以了
    procedure TForm2.Button1Click(Sender: TObject);
    begin
     if unit1.ffff=0 then
      begin
         Edit1.Text:='0';
         end
         else if unit1.ffff=1 then
         begin
         ShowMessage(IntToStr(unit1.ffff));
         edit1.Text:='1';
         Application.ProcessMessages;
         end;  end;
      

  2.   

    谢谢2楼的大虾。
    可是我是想把ffff的值传到form2去,form2的edit也只是用来验证是否传递过去了的。
    还是找不到传递的方法!
      

  3.   

    首先说明一下,全局变量的引用不需要加上单元名称,例如只需引用ffff即可,不需要unit1.ffff其次,说一下解决方法:楼主应该记住,FormCreate中一般是用来“初始化”的,最好不要用来作“显示”用。1、把显示ffff值的代码放在一个函数中,例如加入一个Button,然后把它的Visible置为False,在它的单击代码中写代码:
    Edit1.Text := IntToStr(ffff);2、在unit2的OnShow事件中调用Button单击事件代码即可。说明:
    上述两个步骤只是我用来测试时用的,如果楼主的代码够简单,那么根本不需要另写一个函数(我上面是一个按钮单击事件函数),可以直接放在OnShow事件代码中。
      

  4.   

    现在他的全局变量改了?不是一直都是1吗?是不是只是FormCreate中一般是用来“初始化”的,最好不要用来作“显示”用
      

  5.   

    问题的关键点是 Form2 的 FormCreate Form创建时被调用, 而 Form2 创建时, 此时 Unit1.ffff 的值为0
      

  6.   

    把ffff的初始化放到unit1的initialization部分就可以了。
      

  7.   

    你的Form2是自动创建的,他的Create事件早于Form1的ButtonClick,去Project->Options里面把Form2从自动列表里面去掉,Form1的Button1Click改为begin
      ffff := 1;
      Form2 := TForm2.Create;
      Form2.ShowModal;
    end;