Solved

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

Posted on 2004-09-01
5
239 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
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 50 total points
Comment Utility
try doing it in reverse order

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

Author Comment

by:tiggermark
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yes... that's what access violations are all about... trying to access something that doesn't exist
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

12 Experts available now in Live!

Get 1:1 Help Now