KodeZwerg, Thanks a lot for that and your time.
I tried it but it only lists the components on Form2
So I broke the idea down a bit to try and figure out what is going on, with this little bit of code it is also only listing components from Form2.
I really do not understand the hierarchy, is it like the object inspector where there are branches. Also what is this Pred() I could not find it anywhere.
procedure TForm2.Button6Click(Sender: TObject);
var
i,x: Integer;
begin
for x := 0 to Pred(Application.ComponentCount) do
if (Pos('Form1', Application.Components[x].Name) > 0) then // we searching for a form with a name of "Form1"
for i := 0 to ComponentCount-1 do
Memo2.Lines.Add(Components[i].Name);
end;
procedure TForm2.Button1Click(Sender: TObject);
// remember, the search is case sensitive, you can use LowerCase() or UpperCase() to make it more generic working
// updated to work with Tag instead of Name for target labels
type
TLabels = array of TLabel;
var
x, y, z: Integer;
arr: TLabels;
begin
// initialize empty array
arr := nil;
SetLength(arr, 0);
// iterate over root of application to find a specific form where we wanna change something
for x := 0 to Pred(Application.ComponentCount) do
// we searching for a form with a name of "Form1"
if (Pos('Form1', Application.Components[x].Name) > 0) then
// iterate over found form
for y := 0 to Pred((Application.Components[x] as TForm).ComponentCount) do
// exclude anything that is not a TLabel and find labels with a "Target" inside name or a matching Tag, just use one(!) way
if (((Application.Components[x] as TForm).Components[y] is TLabel) and
// compare against a value
(((Application.Components[x] as TForm).Components[y] as TLabel).Tag = 666)) then
// compare against a name
// (Pos('Target', (Application.Components[x] as TForm).Components[y].Name) > 0)) then
// we found something, add it to the internal array
begin
z := Length(arr);
SetLength(arr, Succ(z));
arr[z] := ((Application.Components[x] as TForm).Components[y] as TLabel);
end;
// when we found something
if (Length(arr) > 0) then
begin
// pick one random and rename the caption to an integer
x := Random(Length(arr));
arr[x].Caption := IntToStr(Succ(x));
// optional: we do not need the array anymore, kill it (it will get destroyed automagical when running out of scope)
SetLength(arr, 0);
arr := nil;
end;
end;