将OnClick命令发送到TabSheet上的所有Dynamic TColorButtons

时间:2012-12-11 17:20:54

标签: 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;

CustomButtonClick代码:

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;

1 个答案:

答案 0 :(得分:3)

你正在调用recurll的方法PingAllClick ...看你调用TColorButton(发件人)。点击

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