delphi - 将 OnClick 命令发送到 TabSheet 上的所有 Dynamic TColorButton

标签 delphi delphi-7

我正在开发一个 PingTool,我有一个动态创建按钮的 TabSheet(根据用户输入从 1 到 150 的任意位置),我希望能够将 OnClick 命令传递给给定的所有按钮标签表。我的个人按钮单击成功运行了我的 ping 代码,但在单击我的 PingAll 按钮时收到 EStackOverflow 消息。任何帮助将不胜感激。代码摘录如下:

用于创建按钮的代码:

begin
  For x := 0 to CheckListBox1.Items.Count -1 Do
  Begin
  If CheckListBox1.Checked[x]=true then
    begin
      GLCount := (GLCount +1);
      theIP :=(CheckListBox1.Items.Strings[x]);
        if GLcount < 10 then begin
          B := TColorButton.Create(Self);
          B.Name:= ('BTN'+intToStr(GLCount+1));
          B.Caption := theIP;
          B.Parent := TabSheet2;
          B.Height := 25;
          B.Width := 97;
          B.Left := 0 + GLCount * 96;
          B.Top := 8;
          B.BackColor := clBtnFace;
          B.ForeColor := clBtnText;
          B.OnClick := CustomButtonClick;
         end;

自定义按钮点击代码:

Procedure TForm1.CustomButtonClick(Sender: TObject);
begin
  GlobalIP:=TColorButton(Sender).caption;
  IdIcmpClient1.Host := GlobalIP;
  IdIcmpClient1.ReceiveTimeout := 500;
  IdIcmpClient1.Ping();

case IdIcmpClient1.ReplyStatus.ReplyStatusType of
  rsEcho:
    TColorButton(Sender).BackColor := clGreen;
  rsTimeOut:
    TColorButton(Sender).BackColor := clRed;
end;
end;

PingAll 代码(不起作用):

procedure TForm1.PingAllClick(Sender: TObject);
var
i: integer;

begin
  For i := 0 to TabSheet2.ControlCount -1 do
    if TabSheet2.Controls[i] is TColorButton then
    begin
    TColorButton(Sender).Click;
end;
end;

最佳答案

您正在调用递归方法 PingAllClick...看看您调用的是 TColorButton(Sender).Click

....
Control := tabSheet2.Controls[i]
if Control  is TColorButton then
  TColorButton(Control ).Click()
....

关于delphi - 将 OnClick 命令发送到 TabSheet 上的所有 Dynamic TColorButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13825259/

相关文章:

delphi - 在 MS Exchange Server 2007 中使用 [delphi] MadExcept 错误处理

delphi - delphi XE2中如何检查目录是否存在?

delphi - 自动化服务器使用 CreateOleObject 启动两次,但并非每次都启动

delphi - 拖动节点时 TTreeView 选择故障

delphi - 无法从 ttcpserver 向 ttcpclientserver 发送文本(字符串)

delphi - 如何以编程方式找出BDE共享存储区的实际位置和大小?

delphi - 同一应用程序的两个不同版本

delphi - Delphi 中经常使用的前缀表示法是否有不同的名称?

delphi - delphi如何自动完成?

delphi - Delphi 7 上的 Quicktime 7