当我在自己的 OnClick 处理程序中销毁按钮时,为什么我的程序会崩溃?

发布于 2024-09-11 17:26:26 字数 1095 浏览 1 评论 0原文

我尝试了我运行的网站上的脚本 http://www.delphi-central.com/runtime.aspx 并成功。


private
  { Private declarations }
  procedure CustomButtonClick(Sender: TObject);

procedure TForm1.AddNewButtonClick(Sender: TObject);
var
  NewButton : TButton;
begin 
  NewButton := TButton.create(self);

  with NewButton do
  begin
    Top    := 30;
    Width  := 60;
    Left   := Width * (self.ControlCount-2);
    Parent := self;
    OnClick := CustomButtonClick;
    Caption := 'Button '+ inttostr (self.ControlCount-2);
  end;  //With
end;

procedure TForm1.DeleteLastButtonClick(Sender: TObject);
begin
  if Self.ControlCount>2 then
    TButton (Controls[ControlCount-1]).destroy;
end;

procedure TForm1.CustomButtonClick(Sender: TObject); 
begin    
    ShowMessage(TButton(Sender).caption + ' Pressed'); 
end;

但如果我更改 OnClick,

OnClick := CustomButtonClick; ==> OnClick := DeleteLastButtonClick;

它将生成一条错误消息。 怎么会这样……???

I tried a script from a web site I run
http://www.delphi-central.com/runtime.aspx and succeed.


private
  { Private declarations }
  procedure CustomButtonClick(Sender: TObject);

procedure TForm1.AddNewButtonClick(Sender: TObject);
var
  NewButton : TButton;
begin 
  NewButton := TButton.create(self);

  with NewButton do
  begin
    Top    := 30;
    Width  := 60;
    Left   := Width * (self.ControlCount-2);
    Parent := self;
    OnClick := CustomButtonClick;
    Caption := 'Button '+ inttostr (self.ControlCount-2);
  end;  //With
end;

procedure TForm1.DeleteLastButtonClick(Sender: TObject);
begin
  if Self.ControlCount>2 then
    TButton (Controls[ControlCount-1]).destroy;
end;

procedure TForm1.CustomButtonClick(Sender: TObject); 
begin    
    ShowMessage(TButton(Sender).caption + ' Pressed'); 
end;

But if I change the OnClick,

OnClick := CustomButtonClick; ==> OnClick := DeleteLastButtonClick;

it will generate an error message.
How could this happen ...???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢烬 2024-09-18 17:26:26

当然,它会爆炸——这就是当你砍断你所坐的树枝时可能发生的情况。

您无法在由该控件生成的事件处理程序内杀死该控件。

请注意,您正在使用的示例没有将 CustomButtonClick 指向删除例程!

Of course it goes boom--that's what's liable to happen when you cut off the branch you're sitting on.

You can't kill a control inside an event handler spawned by that control.

Note that the sample you're working from did NOT point the CustomButtonClick at the delete routine!

从此见与不见 2024-09-18 17:26:26

事件处理程序由控件对象上的函数调用,一旦事件处理程序完成,它可能会执行更多代码。如果删除该控件,则引用该对象的任何代码都可能会引发访问冲突。

您需要做的是让您的程序在完成当前正在运行的所有代码之后删除该控件。为此,您需要发布消息。如果您不了解消息,这是一个学习的好机会。

您需要创建一个新的消息类型 ID。 WM_USER + 1 应该可以。参数之一是要删除的控件的地址。在表单上设置一个消息处理程序,用于处理该消息类型并释放消息参数中引用的控件。然后在事件处理程序中,让它将该消息 PostMessage 到您的表单。这应该可以正常工作,而不会导致访问冲突。

An event handler is called by a function on the control's object, and it could have more code to execute once the event handler finishes. If you delete the control, then any code that references that object is likely to raise an access violation.

What you need to do is get your program to delete the control after it's done with all the code it's currently running. For that, you need to post a message. If you don't know about messages, this is a good opportunity to learn.

You need to create a new message type ID. WM_USER + 1 should work. One of the params will be the address of the control to be deleted. Set up a message handler on your form that handles that message type and frees the control referenced in the message param. And then in the event handler, have it PostMessage that message to your form. That should work without causing access violations.

烟花易冷人易散 2024-09-18 17:26:26

当您认为系统必须在释放鼠标按钮/按键后以某种方式重绘按钮时,很容易想到原因。由于您在单击期间已经删除了按钮对象,因此这将失败。

因此,您需要找到一种方法,在 onClick 事件处理发生并成功完成后以某种方式删除按钮。

It is easy think see the reason, when you consider that the system must somehow redraw the button after you release the mouse button / key. Since you're deleting the button object already during the click, this will fail.

Hence you need to find a way to somehow delete the button after the processing of the onClick event has occurred and successfully finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文