Control Iteration : Collection was modified; enumeration operation may not execute.

Posted on 2007-07-24
Last Modified: 2013-11-07
I'm trying to iterate through a pages controls, and while currently i've been successful setting controls to read only, i'd like to convert them to labels, to make for easy printing. Here is what i have, but when i run it, i get Collection was modified; enumeration operation may not execute.
I know that i'm adding/removing controls so that's why this is happening, but is there a better way to do this then taking the contents of the control, throwing it into a label, removing the original control and then adding the label control to the page?

Public Shared Sub SetFormToReadOnly(ByVal ctrl As System.Web.UI.Control)
            Dim txtTemp As TextBox
            Dim ddltemp As DropDownList
            Dim btnTemp As Button
            'iterate through each control in the page, and each controls' controls to get every
            'control on the masterpage/content page/ and and associated grouped controls, i.e. panels, etc.
            For Each subCtrl As Control In ctrl.Controls
                If TypeOf subCtrl Is TextBox Then
                    txtTemp = subCtrl
                    txtTemp.ReadOnly = True
                    Dim tlabel As New Label
                    tlabel.Text = txtTemp.Text
                ElseIf TypeOf subCtrl Is DropDownList Then
                    ddltemp = subCtrl
                    ddltemp.Enabled = False
                ElseIf TypeOf subCtrl Is Button Then
                    btnTemp = subCtrl
                    btnTemp.Enabled = False
                End If
            Next subCtrl
        End Sub
Question by:ramrodcar
    LVL 1

    Expert Comment

    I have a page that is divided into panels and then I mark the panel enabled property to false (for certain conditions).  The result is, every control inside of that panel is disabled.  Its kind of an easy blanket thing that works.  The only down side I dont like is that everything is grey, rather than black.  I havent played with styles to see if this is changeable.  That is a solution that doesnt involve changing textboxes to labels, or vice versa.
    LVL 96

    Accepted Solution

    You can't use For Each if you are going to remove controls, you would need to loop through the controls by index:

    For index As Integer = 0 To ctrl.Controls.Count - 1
       Dim subCtrl As Control = ctrl.Controls(index)
    Next index


    LVL 22

    Assisted Solution

    As Bob said, you need to use an index. However, if you are going to remove some, you have to index from the end to the start or you will skip some. So the index needs to be decremented after each pass through the loop.
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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