Solved

Problem with ControlCount

Posted on 2000-03-17
6
368 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi with SQL Natvie Client 15 86
Simple Delphi Question 9 83
Delphi...Split view - idea? 1 42
Multi-layered image in FireMonkey 9 18
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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