Learn how to a build a cloud-first strategyRegister Now


VB.NET 2003 - form controls automatic setting of parent

Posted on 2004-11-12
Medium Priority
Last Modified: 2010-04-17
I have a form in VB.NET that has many labels and text boxes and what not.  I need to set the parent of each one to a picture box that will be in the background.  Rather than set each one, as in Me.Label1.Parent = Me. Picturebox1, is there a way to do it programmatically?  I've tried this:

        Dim ccnt As Integer = Me.Controls.Count
        For i = 0 To ccnt - 1
            Me.Controls.Item(i).Parent = Me.PictureBox2

The problem is that the number of controls, ccnt, is at 58 when first initialized.  When the loop runs, the number of controls gradually decreases.  Why?  The index eventually gets out of range and aborts.  Is there a way to do this or do I have to set each one in the load routine?
Question by:ddepuemd

Accepted Solution

dttri earned 500 total points
ID: 12564709
  The number of controls decrease because when you set a control new parent, it's old parent is not its parent anymore, so the decrease.
  I think you could use this:

        Dim ccnt As Integer = Me.Controls.Count
        For i = ccnt - 1 to 0 step -1
            Me.Controls.Item(i).Parent = Me.PictureBox2
LVL 48

Expert Comment

ID: 12564794
Don't forget also to exclude PictureBox2 itself from this enumeration.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Screencast - Getting to Know the Pipeline

810 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