title: Why does WinForm Designer display message "Value cannot be null. Parameter name: context"?
body: <p>I have a c# Windows Forms application, using .NET 3.5.</p>
  
  <p>My machine environment is as follows:</p>
  
  <ul>
  <li>Visual Studio 2010 Premium, SP1</li>
  <li>Windows 7 Professional, SP1</li>
  <li>32-bit processor</li>
  </ul>
  
  <p>I have a <strong>UserControl</strong>-derived custom control that contains a few other UserControls.</p>
  
  <p>I bring up the main UserControl in the Windows Forms Designer in Visual Studio, then I start to click on the various embedded UserControls and randomly change some properties, like <strong>Text</strong>. I then click the <strong>Save</strong> toolbar button. I then continue to make more changes to the properties of the controls, and click <strong>Save</strong>.</p>
  
  <p>After some point in time (and this is not predictable), Visual Studio will display an error dialog.</p>
  
  <p>The WinForms designer displays a modal dialog with a title of "<strong>Microsoft Visual Studio</strong>", and a two-line message, "<strong>Value cannot be null. Parameter name: context</strong>", and an OK button.</p>
  
  <p>Unfortunately, after dismissing this dialog by clicking the OK button, Visual Studio has now corrupted my Form's designer-generated code. The only way to restore my code is from a backup file.</p>
  
  <p>The only workaround I've found is to set <strong>Localizable</strong> to <strong>false</strong> for the UserControl in the <strong>Properties</strong> window. This is not a long term workaround since I need this UserControl to be localizable in our production code.</p>
  
  <p>My project is fairly simple, no web services, etc.</p>
  
  <p>I've tried debugging using another instance of Visual Studio to see if I could catch any exceptions, but no exceptions are generated. There is no debug output of any importance.</p>
  
  <p>Has any one else seen this issue? Looking for suggestions.</p>
  
  <p><strong>More Information...</strong></p>
  
  <p>I was able to successfully attached a second instance of Visual Studio and determine that indeed an exception was being thrown. (I had to uncheck the "Just My Code" debugging option, and enable the symbol server, etc.).</p>
  
  <p>Apparently, when I click <strong>Save</strong> in the WinForm Designer, a <strong>System.ArgumentNullException</strong> is occuring in <strong>System.dll</strong>, in method <strong>System.ComponentModel.Design.Serialization.ContextStack.Push( object context )</strong>. So, the <strong>context</strong> parameter is null, which is the problem. Still no clue why this is happening.</p>
  
  <p>Here is the stack trace:</p>
  
  <blockquote>
    <p>System.dll!System.ComponentModel.Design.Serialization.ContextStack.Push(object context) + 0x88 bytes<br>
        System.Design.dll!System.ComponentModel.Design.Serialization.ResourceCodeDomSerializer.ApplyCacheEntry(System.ComponentModel.Design.Serialization.IDesignerSerializationManager manager = {System.ComponentModel.Design.Serialization.DesignerSerializationManager}, System.ComponentModel.Design.Serialization.ComponentCache.Entry entry) + 0x10e bytes<br>
        System.Design.dll!System.ComponentModel.Design.Serialization.ComponentCodeDomSerializer.Serialize(System.ComponentModel.Design.Serialization.IDesignerSerializationManager manager = {System.ComponentModel.Design.Serialization.DesignerSerializationManager}, object value = {ReveilleSystem.UserInterface.BoldLabel}) + 0xf64 bytes<br>
        System.Design.dll!System.Windows.Forms.Design.ControlCodeDomSerializer.Serialize(System.ComponentModel.Design.Serialization.IDesignerSerializationManager manager = {System.ComponentModel.Design.Serialization.DesignerSerializationManager}, object value = {ReveilleSystem.UserInterface.BoldLabel}) + 0x83 bytes<br>
        System.Design.dll!System.ComponentModel.Design.Serialization.CodeDomSerializerBase.SerializeToExpression(System.ComponentModel.Design.Serialization.IDesignerSerializationManager manager = {System.ComponentModel.Design.Serialization.DesignerSerializationManager}, object value = {ReveilleSystem.UserInterface.BoldLabel}) + 0x173 bytes<br>
        System.Design.dll!System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Serialize(System.ComponentModel.Design.Serialization.IDesignerSerializationManager manager = {System.ComponentModel.Design.Serialization.DesignerSerializationManager}, object root = {ReveilleSystem.UserInterface.WizardEditors.EditorBase}, System.Collections.ICollection members = {System.ComponentModel.ComponentCollection}) + 0x195 bytes<br>
        System.Design.dll!System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformFlush(System.ComponentModel.Design.Serialization.IDesignerSerializationManager manager = {System.ComponentModel.Design.Serialization.DesignerSerializationManager}) + 0x7e bytes<br>
        System.Design.dll!System.ComponentModel.Design.Serialization.BasicDesignerLoader.Flush() + 0x111 bytes<br>
        Microsoft.VisualStudio.Design.dll!Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.Flush() + 0xa0 bytes<br>
        System.Design.dll!System.ComponentModel.Design.DesignSurface.Flush() + 0x21 bytes<br>
        Microsoft.VisualStudio.Shell.Design.dll!Microsoft.VisualStudio.Shell.Design.DesignerWindowPane.OnFlush() + 0x33 bytes<br>
        Microsoft.VisualStudio.Shell.Design.dll!Microsoft.VisualStudio.Shell.Design.DesignerWindowPane.Microsoft.VisualStudio.Shell.Interop.IVsBatchUpdate.FlushPendingUpdates(uint reserved) + 0x2a bytes<br>
        Microsoft.VisualStudio.Editor.Implementation.dll!Microsoft.VisualStudio.Editor.Implementation.VsTextBufferAdapter.Microsoft.VisualStudio.Shell.Interop.IVsBatchUpdate.FlushPendingUpdates(uint dwReserved) + 0x97 bytes 
        Microsoft.VisualStudio.Editor.Implementation.dll!Microsoft.VisualStudio.Editor.Implementation.VsTextBufferAdapter.Save(string pszFilename = "C:\Temp\Reveille_KofaxMFP - Version 2\Editors\Connectivity.Designer.cs", int fRemember = 1, uint nFormatIndex = 0) + 0x2f bytes<br>
        [Native to Managed Transition]<br>
        msenv.dll!DllUnregisterServer()  + 0x10c990 bytes<br>
        [Frames below may be incorrect and/or missing, no symbols loaded for msenv.dll] 
        msenv.dll!DllUnregisterServer()  + 0x10cce8 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x3e1d6 bytes<br>
        [Managed to Native Transition]<br>
        Microsoft.VisualStudio.Editor.Implementation.dll!Microsoft.VisualStudio.Editor.Implementation.VsDocDataAdapter.SaveDocData(Microsoft.VisualStudio.Shell.Interop.VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew, out int pfSaveCanceled) + 0x7e bytes 
        [Native to Managed Transition]<br>
        csproj.dll!DllGetClassObject()  + 0x14e87 bytes 
        msenv.dll!DllUnregisterServer()  + 0x12cbc9 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x12c940 bytes<br>
        msenv.dll!VStudioTerm()  + 0x4ab22 bytes<br>
        msenv.dll!VStudioMain()  + 0x1bb97 bytes<br>
        msenv.dll!VStudioMain()  + 0x1b74c bytes<br>
        msenv.dll!VStudioTerm()  + 0x3ba6d bytes<br>
        msenv.dll!VStudioMain()  + 0x1b8e1 bytes<br>
        [Managed to Native Transition]<br>
        Microsoft.VisualStudio.Shell.Design.dll!Microsoft.VisualStudio.Shell.Design.Serialization.DesignerDocDataService.LockedDocData.Save() + 0xbc bytes<br>
        Microsoft.VisualStudio.Shell.Design.dll!Microsoft.VisualStudio.Shell.Design.Serialization.DesignerDocDataService.Microsoft.VisualStudio.Shell.Interop.IVsRunningDocTableEvents.OnAfterSave(uint docCookie) + 0x124 bytes<br>
        [Native to Managed Transition]<br>
        msenv.dll!DllUnregisterServer()  + 0xd6984 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0xd68f0 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x12cc40 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x12c940 bytes<br>
        msenv.dll!VStudioTerm()  + 0x4ab22 bytes<br>
        msenv.dll!VStudioMain()  + 0x1bb97 bytes<br>
        msenv.dll!VStudioMain()  + 0x1b74c bytes<br>
        msenv.dll!VStudioTerm()  + 0x3ba6d bytes<br>
        msenv.dll!VStudioMain()  + 0x1b8e1 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x34e5c bytes<br>
        msenv.dll!DllMain()  + 0x5c5 bytes<br>
        msenv.dll!DllMain()  + 0x2609d bytes<br>
        msenv.dll!DllMain()  + 0x265a7 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x170768 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x1e56f8 bytes<br>
        msenv.dll!DllMain()  + 0x1ea7f bytes<br>
        msenv.dll!DllMain()  + 0x1e70b bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x170894 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x378a71 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x378bb8 bytes<br>
        msenv.dll!DllUnregisterServer()  + 0x378aa1 bytes<br>
        msenv.dll!6861124f()<br>
        [Managed to Native Transition]<br>
        Microsoft.VisualStudio.Shell.10.0.dll!Microsoft.Internal.VisualStudio.PlatformUI.DataSource.Invoke(string verb, object pvaIn, out object pvaOut) + 0x21 bytes<br>
        Microsoft.VisualStudio.Shell.UI.Internal.dll!Microsoft.VisualStudio.PlatformUI.VsCommand.Execute(object parameter) + 0x29 bytes 
        PresentationFramework.dll!MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(System.Windows.Input.ICommandSource commandSource, bool userInitiated) + 0xb4 bytes<br>
        PresentationFramework.dll!System.Windows.Controls.Primitives.ButtonBase.OnClick() + 0x54 bytes<br>
        PresentationFramework.dll!System.Windows.Controls.Button.OnClick() + 0x4d bytes 
        PresentationFramework.dll!System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e) + 0x9e bytes<br>
        PresentationCore.dll!System.Windows.UIElement.OnMouseLeftButtonUpThunk(object sender, System.Windows.Input.MouseButtonEventArgs e) + 0x6c bytes 
        PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) + 0x31 bytes<br>
        PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) + 0x29 bytes<br>
        PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) + 0x3e bytes<br>
        PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {Microsoft.VisualStudio.PlatformUI.VsButton}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}, bool reRaised = true) + 0xbe bytes<br>
        PresentationCore.dll!System.Windows.UIElement.ReRaiseEventAs(System.Windows.DependencyObject sender = {Microsoft.VisualStudio.PlatformUI.VsButton}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}, System.Windows.RoutedEvent newEvent) + 0x114 bytes<br>
        PresentationCore.dll!System.Windows.UIElement.OnMouseUpThunk(object sender, System.Windows.Input.MouseButtonEventArgs e) + 0xc5 bytes<br>
        PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) + 0x31 bytes<br>
        PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) + 0x29 bytes<br>
        PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) + 0x3e bytes<br>
        PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {Microsoft.VisualStudio.PlatformUI.VsButton}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}, bool reRaised = false) + 0xbe bytes<br>
        PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {Microsoft.VisualStudio.PlatformUI.VsButton}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}) + 0x79 bytes 
        PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}) + 0x41 bytes 
        PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args, bool trusted) + 0x2c bytes<br>
        PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() + 0x1ff bytes<br>
        PresentationCore.dll!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs input) + 0x45 bytes 
        PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport) + 0x62 bytes<br>
        PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(System.IntPtr hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel) + 0x2c2 bytes 
        PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(System.IntPtr hwnd = 1837128, MS.Internal.Interop.WindowMessage msg = WM_LBUTTONUP, System.IntPtr wParam = 0, System.IntPtr lParam = 2293872, ref bool handled = false) + 0x67d bytes<br>
        PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(System.IntPtr hwnd = 1837128, int msg = 514, System.IntPtr wParam = 0, System.IntPtr lParam = 2293872, ref bool handled = false) + 0x75 bytes 
        WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd = 1837128, int msg = 514, System.IntPtr wParam = 0, System.IntPtr lParam = 2293872, ref bool handled = false) + 0xbe bytes<br>
        WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) + 0x7d bytes<br>
        WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) + 0x53 bytes 
        WindowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source = {System.Windows.Threading.Dispatcher}, System.Delegate method, object args, int numArgs, System.Delegate catchHandler = null) + 0x42 bytes 
        WindowsBase.dll!System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) + 0xb4 bytes<br>
        WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd = 1837128, int msg = 514, System.IntPtr wParam = 0, System.IntPtr lParam = 2293872) + 0x104 bytes<br>
        user32.dll!gapfnScSendMessage()  + 0x1cf bytes<br>
        user32.dll!gapfnScSendMessage()  + 0x2cf bytes<br>
        user32.dll!gapfnScSendMessage()  + 0x901 bytes<br>
        user32.dll!DispatchMessageW()  + 0xf bytes<br>
        msenv.dll!DllGetClassObject()  + 0x37521 bytes<br>
        msenv.dll!DllGetClassObject()  + 0x375aa bytes<br>
        msenv.dll!DllCanUnloadNow()  + 0x190f bytes 
        msenv.dll!DllCanUnloadNow()  + 0x1840 bytes 
        msenv.dll!DllCanUnloadNow()  + 0x17a1 bytes 
        msenv.dll!DllCanUnloadNow()  + 0x176e bytes 
        msenv.dll!DllCanUnloadNow()  + 0x16b0 bytes 
        msenv.dll!VStudioMain()  + 0x78 bytes<br>
        devenv.exe!2fcb0637()<br>
        devenv.exe!2fcb0fd4()<br>
        devenv.exe!2fcb20d8()<br>
        devenv.exe!2fcb2148()<br>
        devenv.exe!2fcbea7f()<br>
        kernel32.dll!BaseThreadInitThunk()  + 0x12 bytes<br>
        ntdll.dll!RtlInitializeExceptionChain()  + 0xef bytes<br>
        ntdll.dll!RtlInitializeExceptionChain()  + 0xc2 bytes   </p>
  </blockquote>
  
  <p><strong>[Update2]</strong></p>
  
  <p>I think I've found the reason for my problem.</p>
  
  <p>Based on suggestions from SLaks, I reexamined the <strong>X.Designer.cs</strong> file associated with my UserControl. There were several other controls (checkbox, labels, etc) declared in X.Designer.cs, but that did not actually appear in the UI. I suspect that while I was developing my UserControl using the WinForm editor, I probably went thru several cycles of adding/removing child controls, and at some point some of these child controls were orphaned in the X.Designer.cs file. I went back to a non-localized version of my UserControl (<strong>Localizable = False</strong>), then deleted all the orphaned child controls, then recompiled, then open the UserControl in the WinForm Designer, then set Localizable back to True, and saved everything. This seems to be working now. I'll update this in the future if this ends up not being the solution.</p>