Solved

Problem with ControlCount

Posted on 2000-03-17
6
377 Views
Last Modified: 2010-04-04
I have components titled zComp1, zComp2, zComp3, etc.  I want to be able to change the parent of the components, but only those with the letter z.  They are in a ScrollBox, and I want to change the parent to the Panel.

var
    Num: byte;
    ChildControl: TControl;
begin
    for num := 0 to ScrollBox1.ControlCount - 1 do begin
    ChildControl := ScrollBox1.Controls[num];
    if ChildControl.Name[1] = 'z' then
        ChildControl.Parent := Panel1;
    end;

This code works, except not with the Name[1].  If I wrote:

if ChildControl.Name = 'zComp1' then

Then it would work fine.  But I want to check for all components that start with the letter z.  It has a list index out of bounds error.  If I type:

for num := 0 to ScrollBox1.ControlCount - 2 do begin

Then it works also, but it always leaves one of the controls on the ScrollBox.  I don't understand why I am getting the error.  Any help would be appreciated.
0
Comment
Question by:perkley
[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
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2630133
Try this:

var
    Num: byte;
    ChildControl: TControl;
begin
    for num := 0 to ScrollBox1.ControlCount - 1 do begin
    ChildControl := ScrollBox1.Controls[num];
    if Length(ChildControl.Name) > 0 then
      if ChildControl.Name[1] = 'z' then
          ChildControl.Parent := Panel1;
    end;

0
 

Author Comment

by:perkley
ID: 2630150
Nope, that doesn't work either.  If I step through it, it makes it past the Length(ChildControl.Nam) > 0, but for some reason by putting the Name[1], it has an Index Out of Bounds(1).  It is very strange, it is like a bug with the Name[1].
0
 

Author Comment

by:perkley
ID: 2630164
Actually this is the line it is having the error on:

ChildControl := ScrollBox1.Controls[num];

Now, let me explain something further, because this could be the cause of the problem, and I am not sure how to know.

I have components that originally start as Parent of Panel1.  Then I drag and drop, which changes the parent to the ScrollBox.  It works great.  But then I get this error when I try to reset the parent back to the Panel1.  Do I need to pass some other ownership somehow?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
Epsylon earned 50 total points
ID: 2630177
The problem is that ControlCount changes while running the for loop.
Use this instead:

var num: Integer;
begin
  num := 0;
  while num < ScrollBox1.ControlCount do
  begin
    if (Length(ScrollBox1.Controls[num].Name) > 0) and
       (ScrollBox1.Controls[num].Name[1] = 'z') then
      ScrollBox1.Controls[num].Parent := Panel1
    else
      Inc(num);
  end;
end;
0
 

Author Comment

by:perkley
ID: 2630191
Thanks, it seems to be working great now.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2630204
About ownership: Read about the TComponent.Owner property in the Delphi help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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