[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

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!
0
TonyJiz
Asked:
TonyJiz
1 Solution
 
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
 
Wim ten BrinkCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 BrinkCommented:
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 BrinkCommented:
Darn. I'm too late with my better answer. :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now