Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

thanks my friend.
I also understood my mistake now
Avatar of hidrau

ASKER

there is another thread that I opened, if you can, please help on that.
Avatar of hidrau

ASKER

thanks
Avatar of hidrau

ASKER

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?
Avatar of hidrau

ASKER

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

and that is right way... .ComponentCount and Components iterate child components under parent....