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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

695 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