2013-06-19 35 views
-1

我正在Delphi 7中執行此程序,並且使用頁面控制功能,您是否有任何一種方法可以快速重置Check Boxes和Combo Boxes?無需調用每個複選框並更改其屬性?因爲他們在程序中大約有150個複選框,並且不想輸入每個人的名字將其重置爲未選中狀態? 我嘗試使用下面的代碼:在delphi中重置程序

var 
i : Integer; 
cb : TCheckBox; 
cbx : TComboBox; 
begin 
ADOQuery1.SQL.Clear; 
    for i := 1 to (ComponentCount) do 
    Begin 
    if Components[i] is TCheckBox then 
     begin 
     cb := TCheckBox(Components[i]); 
     cb.checked := false; 
     end; 
    if Components[i] is TComboBox then 
     begin 
     cbx := TComboBox(Components[i]); 
     cbx.ItemIndex := -1; 
     end; 
    end; 
End; 

但我得到一個錯誤列出OD界限?任何想法爲什麼?

+1

一堆解決方案的在這裏找到的http:// stackoverflow.com/q/14892793/1699210 – bummi

+5

Components數組屬性是基於0的,但是您的迭代器不是。 –

+0

所有以前給出的答案都顯示了使用'0'到'Count-1'索引。你爲什麼用'1'來計數? – lurker

回答

3

關閉我的頭頂....這應該運行。

procedure ResetControls(aPage:TTabSheet); 
var 
    loop : integer; 
begin 
    if assigned(aPage) then 
    begin 
    for loop := 0 to aPage.controlcount-1 do 
    begin 
     if aPage.Controls[loop].ClassType = TCheckBox then 
     TCheckBox(aPage.Controls[loop]).Checked := false 
     else if aPage.Controls[loop].ClassType = TComboBox then 
     TComboBox(aPage.Controlss[loop]).itemindex := -1; 
    end; 
    end; 
end; 

編輯:更正爲所指出的雷米

+3

您需要使用'Controls'和'ControlCount'而不是'Components'和'ComponentCount'。如果控件是其他控件的子項,如Panels或GroupBoxes,那麼您將不得不循環地循環查看父/子樹。 –

+0

@RemyLebeau謝謝。就像我說的那樣,這是我的頭頂。 –

+0

@Ryan J. Mills如何更改此編程以使用OnChange事件? – 4DaMouf

1

你可以在表單中做這樣的事情:

for i := 0 to ComponentCount-1 do 
    if Components[i] is TCheckBox then begin 
     cb := TCheckBox(Components[i]); 
     cb.checked := false; 
    end; 
end; 
0
procedure ResetControls(Container: TWinControl); 
var 
    I: Integer; 
    Control: TControl; 
begin 
    for I := 0 to Container.ControlCount - 1 do 
    begin 
    Control := Container.Controls[I]; 

    if Control is TCheckBox then 
     TCheckBox(Control).Checked := False 
    else 
    if Control is TComboBox then 
     TComboBox(Control).ItemIndex := -1; 

    //else if ........ other control classes 

    ResetControls(Control as TWinControl); //recursive to process child controls 
    end; 
end;