2012-12-11 23 views
3

我正在使用PingTool,並且我有一個TabSheet動態創建的按鈕(基於用戶輸入,從1-150的任何位置),我希望能夠通過OnClick命令給定TabSheet上的所有按鈕。我單獨的按鈕點擊成功運行我的ping代碼,但是當點擊我的PingAll按鈕時,我收到了EStackOverflow消息。任何幫助將不勝感激。代碼摘錄如下:用於按鈕創建發送OnClick命令到TabSheet上的所有動態TColorButtons

代碼:

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; 
+0

您的表單中是否有IdAntiFreeze或類似組件? – jachguate

+0

我不知道。不幸的是,我仍然非常青睞Delphi,而不是100%確定IdAntiFreeze的功能。我遇到的問題似乎與PingAll ButtonClick的處理有關,而不是實際執行的ping,因爲我的「CustomButtonClick」過程按預期的方式工作,點擊任何動態創建的按鈕 –

回答

3

要調用recurcive的方法PingAllClick ...看起來你叫TColorButton(Sender).Click代替

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

Awesome。看起來這是我的問題。改變了我的底線,現在效果很好。 TColorButton(TabSheet2.Controls [i])。Click; –

+2

好的,我錯過了! :) – jachguate