How to destroy all the labels on a panel, leaving other controls untouched?

Posted on 2004-09-01
Last Modified: 2010-04-05

I have written the following code to destroy only the labels on a panel in a form's Close event.
But when I run it, I get the error:
" Project . . . raised exception class EListError with message 'List index out of bounds(1)'. " 
This seems to occur when the control encountered is not a label.

Am I doing something stupid? Or if this method is not allowed, how do I do what I want to do?

procedure PanLabsDestroy(p:tpanel);
{   This destoys all labels on panel p  }
  for i:=0 to p.controlcount-1 do
    if lab is tlabel then lab.Destroy;

Help appreciated!

Question by:tiggermark
  • 3
  • 2
LVL 13

Accepted Solution

BlackTigerX earned 50 total points
ID: 11955771
try doing it in reverse order

for i:=p.ControlCount-1 downto 0 do

Author Comment

ID: 11955910
Thanks very much. That works.

Can't think why the other doesn't though. It's all a plot!

LVL 13

Expert Comment

ID: 11956305
because you are deleting, so the count decreases... makes sense?

after you delete one, p.ControlCount is messed up, but the original count was for example 5, and now is 4, therefor 5 doesn't exist anymore, and you get an access violation

if you go in reverse order it doesn't matter if the count changes, because you are going towards zero, not towards the original count

Author Comment

ID: 11958157
Yes, I see.

An error occurs because one tries to use a no-longer-existing control.


The points are all yours!
LVL 13

Expert Comment

ID: 11958961
yes... that's what access violations are all about... trying to access something that doesn't exist

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy file in dll not working but working on exe ! 18 88
delphi prevent click fast 2 189
FMX enumerated colours 2 84
Multi-layered image in FireMonkey 9 15
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now