Solved

Problem with ControlCount

Posted on 2000-03-17
6
380 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
Technology Partners: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

687 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