Solved

Problem with ControlCount

Posted on 2000-03-17
6
365 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
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 13

Accepted Solution

by:
Epsylon earned 50 total points
Comment Utility
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
Comment Utility
Thanks, it seems to be working great now.
0
 
LVL 13

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

9 Experts available now in Live!

Get 1:1 Help Now