-1

I am doing this program in Delphi 7 and using a Page-Control do any of you have a quick way of resetting the Check Boxes and Combo Boxes that is op the page ? With out calling each Check Box and changing its Properties ? Because their is about 150 Check Boxes in the program and don't want to type every ones name out to reset it to unchecked ? I Tried to use the following code :

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;

But I get a error List out od Bounds ? Any ideas why ?

0x436f72647265
  • 414
  • 2
  • 8
  • 21

3 Answers3

3

Off the top of my head....This should run.

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;

edit: Corrected as pointed out by Remy

Vivian Mills
  • 2,552
  • 14
  • 19
  • 3
    You need to use `Controls` and `ControlCount` instead of `Components` and `ComponentCount`. And if the controls are children of other controls, like Panels or GroupBoxes, you will have to loop recursively to drill down the parent/child tree. – Remy Lebeau Jun 19 '13 at 21:08
  • @RemyLebeau Thanks. Like I said this was off the top of my head. – Vivian Mills Jun 20 '13 at 05:46
  • @Ryan J. Mills How can I change this programming to work on the OnChange event ? – 0x436f72647265 Jun 21 '13 at 07:07
  • @Kematian I'm not sure you would need to. If you Call the ResetControls function in the OnChange event and pass in the activePage tabsheet it should work for you as is. Your question implies you want to work on individual pages of the pageControl. So I wrote this to work by passing in a tabsheet. – Vivian Mills Jun 21 '13 at 15:33
  • @Ryan J. Mills okay thanks a lot . Will go and work on that consept. – 0x436f72647265 Jun 21 '13 at 17:49
1

You could do something like this within the form:

for i := 0 to ComponentCount-1 do
    if Components[i] is TCheckBox then begin
       cb := TCheckBox(Components[i]);
       cb.checked := false;
    end;
end;
lurker
  • 56,987
  • 9
  • 69
  • 103
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;
Agustin Seifert
  • 1,938
  • 1
  • 16
  • 29