您好,请教一个问题,谢谢(100分)

  • 主题发起人 主题发起人 wjlsmail
  • 开始时间 开始时间
W

wjlsmail

Unregistered / Unconfirmed
GUEST, unregistred user!
Form1 := TForm1.Create(self);
Form1.Caption := '1234';
if Assigned(Form1) then
Application.Terminate;

Form1中已经放置了几个控件,同时这儿也显式的给了Caption,为何还判断为 Nil ?
怎样Form1才能不是 Nil ?
 
这是什么代码?哪来的?怎么不全部贴出来?
 
这代码逻辑有问题,正常应该是先判断后赋值,因为窗体Create时有可能发生异常

 
Assigned(Form1)为True, 表示Form1不为Nil, 你大概理解反了:)
 
procedure TForm1.Button1Click(Sender: TObject);
begin
//没有任何目的,因为我正在学习,只是做个测试而已,以下为全部,窗体
//中一个Label,一个Button,一个OpenDialog

Form1 := TForm1.Create(self);
Form1.Caption := '1234';
if Assigned(Form1) then
Application.Terminate;

OpenDialog1.FileName := 'dd.txt';
OpenDialog1.InitialDir := 'c:/';
OpenDialog1.Filter := '*.txt';
if OpenDialog1.Execute then
Label1.Caption := OpenDialog1.FileName;
end;
 
欢迎大家指导,谢谢
 
Form1 := TForm1.Create(self); //这里有问题,因为Form1赋值,你重新创建
//一个窗体赋值会把原来窗体变量冲掉
Form1.Caption := '1234';
if Assigned(Form1) then //这里, 好像应该是 not Assigned(Form1)
Application.Terminate; //不过,建议你不要再类的方法里引用实例的变量
 
看不懂你的测试目的
 
taninsh 您好,我的测试没有任何别的目的,只是想知道:
在什么样的情况下 ,Form1才不被视为 Nil

tseug 您好, 这两个程序都不能通过:
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1 := TForm1.Create(self);
Form1.Caption := '1234';

if Assigned(Form1) then
Application.Terminate;
end;
-----------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
//没有任何目的,因为我正在学习,只是做个测试而已,以下为全部,窗体
//中一个Label,一个Button,一个OpenDialog

if Assigned(Form1) then
Application.Terminate;
OpenDialog1.FileName := 'dd.txt';
OpenDialog1.InitialDir := 'c:/';
OpenDialog1.Filter := '*.txt';
if OpenDialog1.Execute then
Label1.Caption := OpenDialog1.FileName;
end;

谢谢大家的指导,望继续赐教,谢谢
 
procedure TForm1.Button1Click(Sender: TObject);
var
Form1:TForm1;

begin
//没有任何目的,因为我正在学习,只是做个测试而已,以下为全部,窗体
//中一个Label,一个Button,一个OpenDialog
Form1 := TForm1.Create(self);
Form1.Caption := '1234';

if Assigned(Form1) then
Application.Terminate;
OpenDialog1.FileName := 'dd.txt';
OpenDialog1.InitialDir := 'c:/';
OpenDialog1.Filter := '*.txt';
if OpenDialog1.Execute then
Label1.Caption := OpenDialog1.FileName;
end;
 
ysai 您好, 您也许弄错了。 请看题,谢谢您得回复

谢谢大家的指导,望继续赐教,谢谢
 
你的form1肯定不是nil
 
首先Form1是一个Tform1类型的变量,在工程里有Application.create(form1,Tform1);
给Form1实例化,他的buttonclick又Form1:= Tform1.create(self);
和Application.create(form1,Tform1);一样的作用,此时相当于给一个变量改变值而已

if Assigned(Form1) then 如果为True 说明Form1<>nil
if not assigned(form1) then 如果为True 说明Form1=nil
如果在工程里这样
Application.Initialize;
if Assigned(Form1) then 这里Form1 为nil
Application.terminate;
Application.CreateForm(TForm1, Form1);
Application.Run;
 
jxyghm: 您好,谢谢您的精彩回复。我看到,在Delphi的帮助中是这样解释 Assigned的:
function Assigned(const P): Boolean;
Assigned returns False if P is nil, True otherwise.

我原来一直以为Assigned 返回 True时P为Nil ,看了帮助才知道我错了:),谢谢您,谢谢大家


 
多人接受答案了。
 
后退
顶部