Solved

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

Posted on 2004-09-01
5
241 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wininet read php file from internet issue 6 90
Delphi XE2 application frozen on Windows 10 10 309
Dynamically Created Query 3 54
CheckListBox usage 3 58
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 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

13 Experts available now in Live!

Get 1:1 Help Now