Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-01
5
Medium Priority
?
253 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

650 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