PassePartout

Hi,

I'm getting a "Collection was modified" in a UserControl derived control when doing a ResumeLayout() after setting the Dock property.

If i override OnResize in my control and do not call base.OnResize(), it does not generate an exception (but does not layout too well...).

The stack dump is at the end.

The question: How can i look into any of this to see what collection is causing the problem. With MFC, we could step through most of the code. Now we can't and it's tough to debug errors within the black box.

Am i at least right in thinking either one of my collections or an internal collection was changed whilst itterating through it

Any ideas

tia,

paul

-------- Caution: stack dump folows ----

System.InvalidOperationException was unhandled
Message="Collection was modified after the enumerator was instantiated."
Source="System"
StackTrace:
at System.Collections.Specialized.ListDictionary.NodeEnumerator.MoveNext()
at System.Windows.Forms.Layout.DefaultLayout.ApplyCachedBounds(IArrangedElement container)
at System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize)
at System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args)
at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ContainerControl.OnLayout(LayoutEventArgs e)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at System.Windows.Forms.Control.PerformLayout()
at System.Windows.Forms.Control.ResumeLayout(Boolean performLayout)
at System.Windows.Forms.Control.ResumeLayout()
at DebugConfigTest.DebugConfigTestTop.ConfigUIForRun()
at BASE.TestDefinitionBase.ConfigTestForRun() in D:\Stc\Delsys\Code\Base\TestDefinitionBase.cs:line 113
at DebugConfigTest.DebugConfigTestTop.ConfigTestForRun()
at DebugConfigTest.DebugConfigTestTop.DisplayFirstWindow()
at EW4App.MainForm.LoadTestIntoFrameUI(TestDefinitionBase inSnapPlug) in D:\Stc\Delsys\Code\EW4App\MainForm.cs:line 298
at EW4App.MainForm.StartUpFirstTest() in D:\Stc\Delsys\Code\EW4App\MainForm.cs:line 208
at EW4App.EW4MainProgram.Main() in D:\Stc\Delsys\Code\EW4App\EW4MainProgram.cs:line 27
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



Re: Windows Forms General InvalidOperationException: Collection was modified

PassePartout

After about a day playing with commenting out segments of code, i isolated this to a method where i add many controls to a container (all are userControl derived).

If i commented out the suspendlayout/resumelayout pairs surrounding the adding of child controls, it works. If i do the suspend, as recomended in documentation, it gets the above exception.

go figure.





Re: Windows Forms General InvalidOperationException: Collection was modified

nobugz

Hard to guess from your stack walk how the code that adds controls gets executed. The best way, and the one that the Windows Forms designer uses, is in the constructor, after the InitializeComponent() call.





Re: Windows Forms General InvalidOperationException: Collection was modified

PassePartout

The container in which the child controls go varies in content for the life of the container. users can add/del children as their plotting needs change. So i have to be able to re-populate the list at an arbitrary time.

The code that adds the children used to do a SuspendLayout, clear the controls list of the container, add the controls, and then ResumeLayout, at which time the exception occured. The stack trace comes after the kids have been added and layout resumed.

Maybe i should resume layout after clearing the list, re-suspend it and add the kids. i'll try that too.

Also, I suppose i could nuke the container and start from scratch at each child population change doing the populating at InitComponent time, but it seems kind of inefficient.

i'll look into it.

tnx,

paul





Re: Windows Forms General InvalidOperationException: Collection was modified

nobugz

The trouble with this exception is that it is raised well after the the damage was done. The best way to diagnose it is to first set a breakpoint on ResumeLayout, then set a breakpoint on any code that modifies the controls collection. Wild guess: you have an event handler that modifies the Controls collection and it runs due to events raised by laying out the controls. Resize or LocationChanged jumps to mind.