VB.NET 2003 - form controls automatic setting of parent

Posted on 2004-11-12
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
    LVL 7

    Accepted Solution

      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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    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

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 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

    11 Experts available now in Live!

    Get 1:1 Help Now