Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use Typecasting 1

Status
Not open for further replies.

chillywinter

Programmer
Apr 8, 2012
4
0
0
AU
I want to reset all my check box's to unticked. I can do it with the following code but is there a short hand way to code this using a FOR statement:

procedure TForm1.Button1Click(Sender: TObject);
begin
checkbox1.checked := false;
checkbox2.checked := false;
checkbox3.checked := false;
checkbox4.checked := false;
checkbox5.checked := false;
end;

The following doesn't work:

procedure TForm1.Button1Click(Sender: TObject);
var
I : integer;
begin
for I := 1 to 5 do Tcheckbox(I).checked := false;
end;

 
Also tried this, without luck:

procedure TForm1.Button1Click(Sender: TObject);
var
I : integer;
myname : string;
begin
for I := 1 to 5 do
begin
myname := 'checkbox'+inttostr(i);
tcheckbox(myname).Checked := false;
end;
end;
 

No that wont work, the compiler cannot simply relate the name strings to the actual components!

One way you can do this is to use the findcomponent(Name: sting): Tcomponent, function
declare a local variable of type TComponent.

var Comp: Tcomponent

make sure that the check boxes have the same name but with a numeric ending. Similar to what you have.

e.g Checkbox1, Checkbox2, Checkbox3, etc

for a := 1 to 3 do
begin
Comp := Parent.Findcomponent('Checkbox'+ inttostr(a));
Comp.checked := false;
end;

Note, 'parent' should be the form or panel of wherever the components to found are situated. if you don't have this it may not work.

Findcomponent is a very powerful function and can be used in many situations, especially if you are creating things dynamically, rather than just dragging onto your forms.


Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
Supposing that the CheckBoxes are inside a Panel (or other container), you can also try this:

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to Panel1.ControlCount - 1 do
      if Panel1.Controls[i] is TCheckBox then
          (Panel1.Controls[i] as TCheckBox).Checked := true;
end;

or

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to Panel1.ControlCount - 1 do
      if Copy(Panel1.Controls[i].Name, 1, 8) = 'CheckBox' then
          (Panel1.Controls[i] as TCheckBox).Checked := true;
end;

I hope it's useful.


Imoveis em Guarulhos
 
If all your check boxes are declared sequentialy in the form class, you can also do this:
Code:
procedure TForm1.UncheckAll;
type
  TChkBxArr = array[1..5] of TCheckBox;
  PChkBxArr = ^TChkBxArr;
var ChkBoxes:PChkBxArr;
  I:Integer;
begin
  ChkBoxes := PChkBxArr(@checkbox1);
  For I := 1 to 5 DO
    ChkBoxes^[I].Checked := false;
end;
 
The declare array method (see [blue]Prattaratt[/blue]'s solution above) using pointers works well (but requires an array to be defined, and pointers look so ugly). Based on [blue]sggaunt[/blue]'s explanation and through reading the Delphi help file and its FindComponent example, the following code seems to work quiet well:

Code:
procedure TForm1.Button4Click(Sender: TObject);
var
 I : integer;
begin
 for I := 1 to 10 do
 TCheckbox(FindComponent('Checkbox'+ inttostr(I))).checked := false;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top