How to do a For loop with all shapes on a form.

Hey does anyone know how do to a FOR loop with all shapes on a form, for example:

for i:= 0 to Form1.Shape.Count - 1 do
Form1.Shape[i].Brush.Color := clWhite;

This will paint all of the already added shapes on the form white.
Thnx!
TonyJizAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jpedefCommented:
procedure Form1.SetShapeBrushColor(NewColor: TColor);
var
  i: integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] is TShape then
      TShape(Components[i]).Brush.Color := NewColor;
  end;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wim ten BrinkSelf-employed developerCommented:
procedure TFormMain.FormClick( Sender: TObject );
var
  I: Integer;
  Shape: TShape;
begin
  for I := 0 to Pred( ComponentCount ) do begin
    if ( Components[ I ] is TShape ) then begin
      Shape := ( Components[ I ] as TShape );
      // Do something with Shape. (But don't free it!)
    end;
  end;
end;

Something like that, you mean? Just enumerate all components on your form and if it's a TShape then you can do stuff with it.
0
esoftbgCommented:
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  I:      Integer;
begin
  for I := 0 to ComponentCount-1 do
    if (Components[I] is TShape) then
      TShape(Components[I]).Brush.Color := clWhite;
end;
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

kretzschmarCommented:
ts ts ts, what a run :-))

well an alternative, if you have not adjusted the automated naming

var shape : TShape;
begin
  for i := 1 to NumOfShapes do
  begin
    shape := findComponent('Shape'+inttostr(i));
    if assigned(shape) then
      Shape.Brush.Color := WhatEver;
  end;
end;

meikl ;-)
0
TonyJizAuthor Commented:
Thnx people!
0
Wim ten BrinkSelf-employed developerCommented:
Heh, I just thought of something... What if these shapes aren't components to begin with? :-)

Let's say he has a dynamic array of shapes. He would then have to use:

for I := Low(Shapes) to High(Shapes) do Shape[I].Brush.Color := Whatever;

But no, there's a component called TShape... And TShape is a control so let me give a better answer!

var
  I: Integer;
begin
  for I := 0 to Pred( ControlCount ) do begin
    if ( Controls[ I ] is TShape ) then ( Controls[ I ] as TShape ).Brush.Color := clWhite;
  end;

or:
 
  for I := 0 to Pred( Form1.ControlCount ) do begin
    if ( Form1.Controls[ I ] is TShape ) then ( Form1.Controls[ I ] as TShape ).Brush.Color := clWhite;
  end;


Using the controls list is better since it's a smaller list than the Components list. It only contains the controls and TShape happens to be one.
The second one should be used if you're NOT executing the code from within a form method, the first one should be within one of the form methods or events.

Alex ;-P
0
Wim ten BrinkSelf-employed developerCommented:
Darn. I'm too late with my better answer. :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.