?
Solved

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

Posted on 2004-09-01
5
Medium Priority
?
252 Views
Last Modified: 2010-04-05
Hi

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  }
var
  i:integer;
  lab:tcontrol;
begin
  for i:=0 to p.controlcount-1 do
  begin
    lab:=p.controls[i];
    if lab is tlabel then lab.Destroy;
  end;
end;

Help appreciated!

tiggermark
0
Comment
Question by:tiggermark
[X]
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
5 Comments
 
LVL 13

Accepted Solution

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

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

Author Comment

by:tiggermark
ID: 11955910
Thanks very much. That works.

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

tiggermark
0
 
LVL 13

Expert Comment

by:BlackTigerX
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
0
 

Author Comment

by:tiggermark
ID: 11958157
Yes, I see.

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

Thanks.

The points are all yours!
0
 
LVL 13

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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