自删除按钮

发布于 2025-01-29 04:57:07 字数 1174 浏览 4 评论 0 原文

我有一个带有一堆tpanels的TSCrollbox,并在运行时生成了一些TBUTTON。 当一个单击一个tbutton时,我需要删除TPANEL,但是在onClick结束时在访问违规中进行此操作...

procedure TMainForm.ButanClick(Sender: TObject);
var
  vParentPanel: TPanel;
begin
  if (string(TButton(Sender).Name).StartsWith('L')) then
  begin
    TButton(Sender).Caption := 'YARE YARE DAZE';
  end
  else
  begin
    vParentPanel := TPanel(TButton(Sender).GetParentComponent());
    TheScrollBox.RemoveComponent(vParentPanel);
    vParentPanel.Destroy();
    // access violation but the panel is removed
  end;
end;

procedure TMainForm.Button3Click(Sender: TObject);
var
  i: Integer;
  vPanel: TPanel;
  vButton: TButton;
begin
  for i := 0 to 20 do
  begin
    vPanel := TPanel.Create(TheScrollBox);
    vPanel.Align := alTop;
    vPanel.Parent := TheScrollBox;

    vButton := TButton.Create(vPanel);
    vButton.Align := alLeft;
    vButton.Parent := vPanel;
    vButton.Name := 'L_butan' + IntToStr(i);
    vButton.OnClick := ButanClick;

    vButton := TButton.Create(vPanel);
    vButton.Align := alRight;
    vButton.Parent := vPanel;
    vButton.Name := 'R_butan' + IntToStr(i);
    vButton.OnClick := ButanClick;
  end;
end;

I have a TScrollBox with a bunch of TPanels with some TButtons generated at runtime.
I need to delete the TPanel when one TButton is clicked but doing that in OnClick end in an access violation...

procedure TMainForm.ButanClick(Sender: TObject);
var
  vParentPanel: TPanel;
begin
  if (string(TButton(Sender).Name).StartsWith('L')) then
  begin
    TButton(Sender).Caption := 'YARE YARE DAZE';
  end
  else
  begin
    vParentPanel := TPanel(TButton(Sender).GetParentComponent());
    TheScrollBox.RemoveComponent(vParentPanel);
    vParentPanel.Destroy();
    // access violation but the panel is removed
  end;
end;

procedure TMainForm.Button3Click(Sender: TObject);
var
  i: Integer;
  vPanel: TPanel;
  vButton: TButton;
begin
  for i := 0 to 20 do
  begin
    vPanel := TPanel.Create(TheScrollBox);
    vPanel.Align := alTop;
    vPanel.Parent := TheScrollBox;

    vButton := TButton.Create(vPanel);
    vButton.Align := alLeft;
    vButton.Parent := vPanel;
    vButton.Name := 'L_butan' + IntToStr(i);
    vButton.OnClick := ButanClick;

    vButton := TButton.Create(vPanel);
    vButton.Align := alRight;
    vButton.Parent := vPanel;
    vButton.Name := 'R_butan' + IntToStr(i);
    vButton.OnClick := ButanClick;
  end;
end;

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

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

发布评论

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

评论(2

做个ˇ局外人 2025-02-05 04:57:07

您不能从 tbutton 's onClick 事件中安全地销毁父 tpanel (或 tbutton 本身)。 VCL仍需要访问 tpanel / tbutton 在事件处理程序退出后进行节拍。因此,您需要将破坏延迟到处理程序退出后。最简单的方法是使用 tpanel 上调用 tobject.free(),例如:

procedure TMainForm.ButanClick(Sender: TObject);
var
  vButton: TButton;
begin
  vButton := TButton(Sender);
  if vButton.Name.StartsWith('L') then
  begin
    vButton.Caption := 'YARE YARE DAZE';
  end
  else
  begin
    TThread.ForceQueue(nil, vButton.Parent.Free);
  end;
end;

tpanel 将从<<<<<<代码> TSCrollbox 在其破坏过程中。您无需手动处理该步骤。

You cannot safely destroy the parent TPanel (or the TButton itself) from inside the TButton's OnClick event. The VCL still needs access to the TPanel/TButton for a beat after the event handler exits. So, you need to delay the destruction until after the handler exits. The easiest way to do that is to use TThread.ForceQueue() to call TObject.Free() on the TPanel, eg:

procedure TMainForm.ButanClick(Sender: TObject);
var
  vButton: TButton;
begin
  vButton := TButton(Sender);
  if vButton.Name.StartsWith('L') then
  begin
    vButton.Caption := 'YARE YARE DAZE';
  end
  else
  begin
    TThread.ForceQueue(nil, vButton.Parent.Free);
  end;
end;

The TPanel will remove itself from the TScrollBox during its destruction. You do not need to handle that step manually.

花之痕靓丽 2025-02-05 04:57:07

重新解决的schaaf 答案:

...
const
WM_REMOVEPANEL = WM_USER + 9001;
procedure ButanClick(Sender: TObject);
procedure OnCustomMessage(var Msg: TMessage); message WM_REMOVEPANEL;

...
procedure TMainForm.ButanClick(Sender: TObject);
var
  vParentPanel: TPanel;
begin
  if (string(TButton(Sender).Name).StartsWith('L')) then
  begin
    TButton(Sender).Caption := 'YARE YARE DAZE';
  end
  else
  begin
    // SendMessage = access violation again because it wait the return
    // while PostMessage return istantly
    PostMessage(Handle, WM_REMOVEPANEL, 0, THandle(@Sender));
  end;
end;

procedure TMainForm.OnCustomMessage(var Msg: TMessage);
var
  vButton: TButton;
begin
  if (Msg.Msg = WM_REMOVEPANEL) then
  begin
    vButton := TButton(Pointer(Msg.LParam)^);
    ShowMessage(vButton.Name);
    TheScrollBox.RemoveComponent(vButton.GetParentComponent());
    TPanel(vButton.GetParentComponent()).Destroy();
    Msg.Result := 1;
  end
  else
    Msg.Result := 0;
end;

Solved with Renate Schaaf answer:

...
const
WM_REMOVEPANEL = WM_USER + 9001;
procedure ButanClick(Sender: TObject);
procedure OnCustomMessage(var Msg: TMessage); message WM_REMOVEPANEL;

...
procedure TMainForm.ButanClick(Sender: TObject);
var
  vParentPanel: TPanel;
begin
  if (string(TButton(Sender).Name).StartsWith('L')) then
  begin
    TButton(Sender).Caption := 'YARE YARE DAZE';
  end
  else
  begin
    // SendMessage = access violation again because it wait the return
    // while PostMessage return istantly
    PostMessage(Handle, WM_REMOVEPANEL, 0, THandle(@Sender));
  end;
end;

procedure TMainForm.OnCustomMessage(var Msg: TMessage);
var
  vButton: TButton;
begin
  if (Msg.Msg = WM_REMOVEPANEL) then
  begin
    vButton := TButton(Pointer(Msg.LParam)^);
    ShowMessage(vButton.Name);
    TheScrollBox.RemoveComponent(vButton.GetParentComponent());
    TPanel(vButton.GetParentComponent()).Destroy();
    Msg.Result := 1;
  end
  else
    Msg.Result := 0;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文