Solved

Problem with ControlCount

Posted on 2000-03-17
6
370 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i search if string exist in array ? 3 59
Dynamically Created Query 3 55
Firemonkey DbLookupComboBox equivalent ? 2 48
DBCtrlGrid, Delphi, Scroll 8 15
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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