******关于全局变量的问题********(1分)

阿bao

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在两个窗口中传递t1这个变量的值
我在第一个单元中这样设
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;
t1:integer;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
t1:=12;
Form2.show;
end;

end.
****************************
我在第二的单元中
unit Unit2;

interface

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

type
TForm2 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
Label1.Caption:=inttostr(Unit1.t1);
end;

end.
******************
为什么我的Lable1总是显示0?????????
 
procedure TForm2.FormCreate(Sender: TObject);
begin
Label1.Caption:=inttostr(t1);
end;
 
Label1.Caption:=inttostr(Unit1.t1);放在Onshow事件中

t1:=12;
Application.CreateForm(TForm2, Form2);
Form2.show;
 
呵呵,不行的,在create事件中用的t1的值是unit1单元中初始化时t1的值,所以一直为0
你可以在show或者其他事件中看看就会是12了
 
为什么要放在ONSHOW事件中,而不能放在FORMCREATE事件中呢?
 
好了,谢谢,解决了
 
FORMCREATE是在窗体窗件时执行,其实在你对t1负值之前窗体就创建了(工程文件中Application.CreateForm(TForm2, Form2);),而Onshow是在Form2.show时执行
 
哦,我懂了,是先执行工程文件里的
Application.CreateForm*********
Application.CreateForm*********
完成后
再执行每个单元里的代码的代码的是么?
恩,谢谢你,给你加分^-^
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
715
import
I
I
回复
0
查看
658
import
I
S
回复
0
查看
771
SUNSTONE的Delphi笔记
S
I
回复
0
查看
611
import
I
顶部