function Test :Boolean;
var
Form1 : TForm1;
begin
if Form1 = nil then
ShowMessage('nil')
else
ShowMessage('not nil');
if Not Assigned(Form1) then
ShowMessage('not assigned')
else
ShowMessage('assigned');
end;在这个例子中,Form1 只是声明了,但是,显示是 assigned 和 nil.问题:
1,上面的情况,请问这是为什么?
2,现在有一Button1,当Click后,想让Form1.Show ,同时,再点Button1,还是显示原先那个Form1,而不是开第二个Form1,请问该如何做?
var
Form1 : TForm1;
begin
if Form1 = nil then
ShowMessage('nil')
else
ShowMessage('not nil');
if Not Assigned(Form1) then
ShowMessage('not assigned')
else
ShowMessage('assigned');
end;在这个例子中,Form1 只是声明了,但是,显示是 assigned 和 nil.问题:
1,上面的情况,请问这是为什么?
2,现在有一Button1,当Click后,想让Form1.Show ,同时,再点Button1,还是显示原先那个Form1,而不是开第二个Form1,请问该如何做?
nil是因为尚未Create
这种变量一般要在CLose or Free时
Form1 :=nil
才能在下次使用时,不出问题。
创建的时间,用
if not assigned(form1) then
application.createform(Tform,Form1);
form1.show;在form1的Onclose事件中,action:=cafree;
在form1的Ondestory事件中,form1:=nil;
Click后,CAPTION为打开二,再CILCK CAPTION 为打开一
然后根据CAPTION 打开窗体
not nil
Assigned
你再试下!!!
没有赋值,能不为nil吗?? ”恩,你看看,没有赋值,结果就是 not nil 了。
var
Form1 : TForm1;
begin
if Form1 = nil then
ShowMessage('nil')
else
ShowMessage('not nil');
if Not Assigned(Form1) then
ShowMessage('not assigned')
else
ShowMessage('assigned');
end;
在这个例子中,Form1 只是声明了,但是,显示是 assigned 和 not nil.
问题:
1,上面的情况,请问这是为什么?
2,现在有一Button1,当Click后,想让Form1.Show ,同时,再点Button1,还是显示原先那个Form1,而不是开第二个Form1,请问该如何做?刚才写错了,所以,估计楼上的几位都分析错了。只有一位兄弟测试了。请问这是怎么回事?该怎么分析?
局部变量 Form1 编译器初始化的时候不会将Form1设置为nil,而是一个随机值
全局变量会置为nil
而你的写法全部是判断是否是nil,所以得到的结果和你想象的不一样
-----------
只要把Form1 声明成全局变量就可以了
1.此时可以用
if not assigned(Form1) then /或者 if Form1=nil
创建
Form1.Show;//Form1.ShowModel; FreeAndNil(Form1);
---------
如果Form1是MDIChild,那么Form1的Close里面写action:caFree;
Form1的Destory里面写Form1:=nil