How to clean all components in a scrollbox?

Hello guys,

something simple but I can't reach it so far.

I create a simple an example, maybe I am doing something wrong in my code that I haven't figured out so far.

Please, take a look at the code

procedure TForm1.sSpeedButton3Click(Sender: TObject);
var
  i, x : Integer;
  L : TLabel;
begin
  x := 10;
  for i := 0 to 20 do
  Begin
    L := TLabel.Create(self);
    L.Parent := ScrollBox1;
    L.Name := 'L' + IntToStr(i);
    L.top := x;
    L.Caption := 'Teste_' + IntToStr(i);
    Inc(x,20);
  End;
end;

procedure TForm1.sSpeedButton4Click(Sender: TObject);
var
 i : Integer;
begin
 for i := ScrollBox1.ComponentCount-1 downto 0 do
  Begin
    if ScrollBox1.Components[i] is TLabel Then
       TLabel(ScrollBox1.Components[i]).Free

  End;

end;

Open in new window


As you can see, first I create some labes in a scrollbox but I can't clear it with my code to free all labels, why?

thanks
Alex
LVL 1
hidrauAsked:
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.

Sinisa VukSoftware architectCommented:
you should set scrollbox as owner too:

L := TLabel.Create(ScrollBox1);
...

Open in new window


and for destroy you can use shorter version:
ScrollBox1.DestroyComponents;

Open in new window

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
hidrauAuthor Commented:
thanks my friend.
I also understood my mistake now
0
hidrauAuthor Commented:
there is another thread that I opened, if you can, please help on that.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

hidrauAuthor Commented:
thanks
0
hidrauAuthor Commented:
Sinisav, something that I noticed,

For labels is ok, but I am having problem with edits.

When I created some Edits.create(scrollbox1) I wasn't able to use this code:

TEdit(Findcomponent('ED0')).setFocus

How can I find my first Edit now?
0
hidrauAuthor Commented:
I got it this way

  for X := 0 to sScrollBox1.ComponentCount-1 do
  Begin
    if UpperCase(sScrollBox1.Components[X].Name) = 'ED0' Then
    Begin
      TsEdit( sScrollBox1.Components[X] ).SetFocus;
      Break;
    End;
  End;

Open in new window

0
Sinisa VukSoftware architectCommented:
and that is right way... .ComponentCount and Components iterate child components under parent....
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.