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

x
?
Solved

How to clean all components in a scrollbox?

Posted on 2014-08-29
7
Medium Priority
?
741 Views
Last Modified: 2014-08-30
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
0
Comment
Question by:hidrau
  • 5
  • 2
7 Comments
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 2000 total points
ID: 40293689
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
 
LVL 1

Author Comment

by:hidrau
ID: 40293715
thanks my friend.
I also understood my mistake now
0
 
LVL 1

Author Comment

by:hidrau
ID: 40293720
there is another thread that I opened, if you can, please help on that.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Closing Comment

by:hidrau
ID: 40293721
thanks
0
 
LVL 1

Author Comment

by:hidrau
ID: 40293827
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
 
LVL 1

Author Comment

by:hidrau
ID: 40293845
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
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 40294813
and that is right way... .ComponentCount and Components iterate child components under parent....
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question