Visual Studio 2022 WinForm Designer throws 'Object reference not set to an instance of an object' exception when loading child Winform Window

KaLun Leung 0 Reputation points
2023-04-28T10:39:42.7766667+00:00

Hi all,

I have a C# project in .Net Framework 4.8 with WinForm as UI. I use upgrade assistent to convert the project from .Net Framework 4.8 to .Net 6.0. The project is able to compile an execute and run.

However, when I try to open the WinForm in Visual Studio 2022 to edit, the the VS2022 WinForm Designer throws "Object reference not set to an instance of an object" and failed to open. This only happens to the WinForm inherited from a custom base form. If the WinForm is not inherited from my base form, it can be opened without exception.

I try to comment out the code in the custom base form but still have the exception throw. Anyone has idea why?

Below is the log from VS2022 WinForm Designer for reference

[18:25:12.364062] info: Creating VsDesignerLoader
[18:25:12.378051] trce: Sending request: Sessions/Create
[18:25:12.380050] trce: Sessions/Create took 00:00:00.0018817.
[18:25:12.380050] trce: Sending request: Options/SetOptions
[18:25:12.381050] trce: Options/SetOptions took 00:00:00.0011473.
[18:25:12.382049] trce: Sending request: Sessions/BeginLoad
[18:25:12.383048] trce: Sessions/BeginLoad took 00:00:00.0007361.
[18:25:12.433543] trce: Sending request: Sessions/DesignerActivated
[18:25:12.433543] trce: Sessions/DesignerActivated took 00:00:00.0007612.
[18:25:12.496667] trce: Sending request: Sessions/FindDesignableClass
[18:25:12.498655] trce: Sessions/FindDesignableClass took 00:00:00.0010668.
[18:25:12.501664] trce: Sending request: Sessions/InitializeRootComponent
[18:25:12.511664] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.511664] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.511664] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.513639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.513639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.513639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.515639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.515639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.515639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.517639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.517639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.517639] info: [XXX.UI]: Falling back to 'Microsoft.DotNet.DesignTools.Designers.ComponentDocumentDesigner' for 'System.Windows.Forms.Design.ComponentDocumentDesigner'
[18:25:12.521636] trce: Sessions/InitializeRootComponent took 00:00:00.0195805.
[18:25:12.521636] trce: Notifying 1 'Components/ComponentRemoved' handlers
[18:25:12.521636] trce: Notifying 1 'Components/ComponentRemoved' handlers
[18:25:12.521636] trce: Notifying 1 'Components/ComponentRemoved' handlers
[18:25:12.521636] trce: Notifying 1 'Components/ComponentRemoved' handlers
[18:25:12.521636] trce: Notifying 1 'Components/ComponentRemoved' handlers
[18:25:12.523634] fail: Request failures: Sessions/InitializeRootComponent.
                        Microsoft.DotNet.DesignTools.Client.DesignToolsServerException: Object reference not set to an instance of an object.

                        For information on how to troubleshoot the designer refer to the guide at https://aka.ms/winforms/designer/troubleshooting.
[18:25:12.529630] trce: Updating selection in server process.
[18:25:12.529630] trce: Sending request: Selection/UpdateSelection
[18:25:12.530638] trce: [XXX.UI]: Attempting to update selection: 0 components, Replace
[18:25:12.530638] trce: [XXX.UI]: Made change: False
[18:25:12.530638] trce: Selection/UpdateSelection took 00:00:00.0006482.
[18:25:12.530638] trce: Sending request: Sessions/Unload
[18:25:12.530638] trce: Sessions/Unload took 00:00:00.0003190.
[18:25:12.530638] trce: Sending request: Sessions/EndLoad
[18:25:12.530638] trce: Sessions/EndLoad took 00:00:00.0003387.
[18:25:14.455274] trce: [XXX.UI]: Sending request to remove roots:
                          - 31: 3 root(s).
                          - 30: 3 root(s).
                          - 2f: 3 root(s).
                          - 2e: 3 root(s).
                          - 32: 3 root(s).
                        
[18:25:14.455274] trce: Sending request: Objects/RemoveObjectRoots
[18:25:14.455274] trce: [XXX.UI]: Objects/RemoveObjectRoots request received for 5 object(s).
                          - 31: 3 root(s).
                          - 30: 3 root(s).
                          - 2f: 3 root(s).
                          - 2e: 3 root(s).
                          - 32: 3 root(s).
                        
[18:25:14.456274] trce: Objects/RemoveObjectRoots took 00:00:00.0010352.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,914 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,307 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,152 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Thiede 0 Reputation points
    2023-08-04T19:06:34.7133333+00:00

    I just encountered the same issue in VS 2022 with a UserControl.

    When I opened the corresponding .resx file of the UserControl, I found an entry with the message "Value cannot be displayed." I removed this entry from the .resx file and then was able to open the UserControl in the VS Designer (no restart needed)

    The erroneous entry in the .resx file was the only one referencing mscorlib, Version=4.0.0.0. All other entries are referencing System.Windows.Forms, Version=4.0.0.0.

    In the Visual Studio Output Log, there was a corresponding note: The conversion exception was: Unable to find assembly 'mscorlib, Version=4.0.0.0.

    Project background Information:

    The C# project was originally developed with .NET Framework 4.8, Windows Forms, and DevExpress, and was migrated to .net6-windows a year ago. I performed the migration to .net6 manually and did not use any specific tools for it. The VS version is Microsoft Visual Studio Professional 2022 (64-bit) - Version 17.6.2. The OS is Windows 10.

    Perhaps this information might be helpful.


  2. reasra 0 Reputation points
    2024-12-21T21:34:42.8066667+00:00

    I encountered the same problem with a c# WPF .net 8 app.

    I resolved it by closing VS, deleting the bin and obj directories, and reloading VS.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.