有一个TBITMAP类型的变量,如何判断它已经CREATE了?

hzzkf 2001-09-07 03:19:14
定义了一个IMG变量,如何在调用下述语句前判断它已被CREATE了?
img := Tbitmap.Create;
...全文
99 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
DIABL0 2001-10-04
  • 打赏
  • 举报
回复
try
img.classinfo;
except
showmessage('not created');
TechnoFantasy 2001-09-07
  • 打赏
  • 举报
回复
在建立对象并free以后一定要设定未nil,例如下面的代码:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
x:TButton;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

begin
x:=TButton.Create(self);
x.parent:=self;
x.free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if assigned(x) then
x.caption:='Error';
end;

end.

在点击button1建立对象并释放后点击Button2会出现错误,这是因为对象free后没有设定
未nil
cobi 2001-09-07
  • 打赏
  • 举报
回复
同意楼上两位意见
MouseBrother 2001-09-07
  • 打赏
  • 举报
回复
使用函数
If Not Assigned(Bitmap变量) Then Bitmap变量 := TBitmap.Create;
chechy 2001-09-07
  • 打赏
  • 举报
回复
如果IMG初始化时就赋值为NIL,那么就非常好判断了。
IF IMG = NIL THEN
IMG := TBITMAP.CREATE;
否则,就比较难判断了。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧