以编程方式创建标签并从表单中删除标签
我在这里得到了一些代码,它可以很好地创建标签,但是当我释放标签时,它仍然显示在表单上。即使它已被删除并且不再“分配”。
这是下面的代码。 它可以很好地创建标签,但不会删除。 无一例外,分配者说假。
我也可以用 TRectangle 重现这一点。
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Lab : TLabel;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
Lab := TLabel.Create(Self);
Lab.Parent := Form1;
Lab.Text := 'Hello!';
Lab.Position.X := 30;
Lab.Position.Y := 40;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FreeAndNil(Lab);
ShowMessage(BoolToStr(Assigned(Lab), true));
end;
I got some code here, which creates a label just fine, but when I free the label it STILL shows on the form. Even though it's been removed and no longer 'assigned'.
Here is the code below.
It creates the label fine, but wont remove.
No exceptions, and the assigned says false.
I can reproduce this with a TRectangle as well.
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Lab : TLabel;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
Lab := TLabel.Create(Self);
Lab.Parent := Form1;
Lab.Text := 'Hello!';
Lab.Position.X := 30;
Lab.Position.Y := 40;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FreeAndNil(Lab);
ShowMessage(BoolToStr(Assigned(Lab), true));
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
可以提供帮助。
i think
could help.
FWIW,使用Delphi XE(不是XE2),您的代码按预期工作(在进行一些小的修改之后,例如用Left 替换Position.X 等)。
需要检查/尝试的一些事情:
FWIW, using Delphi XE (not XE2), your code works as expected (after making some small modifications, e.g. replacing Position.X with Left etc).
A few things to check/try: