67

We have the following shared component:

public class OurServiceBase : System.ServiceProcess.ServiceBase

This class has functionality we want in all our downstream services, such as standardized execution scheduling and logging functionality.

In a new project, I add the following:

public class MyService : System.ServiceProcess.ServiceBase

In the Windows Designer, the class shows properly.

When I change the service to derive from OurServiceBase

public class MyService : OurSharedLibrary.OurServiceBase

The designer stops working:

Error screenshot

The full error is: The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: EmailProcessor --- The base class 'OurSharedLibrary.CienaServiceBase' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

The proper assemblies are referenced, the project builds. I don't understand why the designer is flipping out over this since my service ultimately does derive from a designable class.

Any suggestions would be most welcome.

Bit more information - the call stack from the designer when it renders the error about not being able to design the derived service:

at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host) 

7/19/2011 2:34PM EDT New discovery.

Class "OurServiceBase" exists in a separate project (usually referenced as a DLL only). On a whim, I copied the base class file into my project, built, and opened the designer. It worked! When I removed the base class file again and returned to the external DLL reference, the designer broke again.

The Evil Greebo
  • 7,013
  • 3
  • 28
  • 55
  • 1
    I should add, in no way does this impede my ability to work - I just don't like having the designer freak out. – The Evil Greebo Jul 13 '11 at 17:53
  • 1
    Use fuslogvw.exe to troubleshoot assembly resolution problems. – Hans Passant Jul 13 '11 at 18:25
  • 1
    ıt's under C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools – Orhan Cinar Jul 13 '11 at 18:42
  • 8
    "You can google it as quickly as I can. Take the first hit." Not true. I've Googled this quite a bit and can't find the *correct* answer. There are many answers depending on the VS version and other things, but none I've pursued so far apply to my situation. And unlike The Evil Greebo, this is significantly impeding my progress. Edit: Just found another user who is having this problem. No answer yet. http://dotnetforum.net/topic/23965-the-designer-could-not-be-shown-for-this-file-because-none-of-the-classes-within-it-can-be-designed/ –  Jul 13 '11 at 18:39
  • 1
    I believe that Hans google comment was referring specifically to finding the fuslogvw.exe utility, not the solution to the problem. – Chris Dunaway Jul 13 '11 at 19:08
  • Ah. Of course. Read that several times and missed the chain. Unfortunately, clearing that up doesn't clear up the problem I'm having. I have had the designer work a couple of times, but nothing has changed about the projects. And rebuilding fixed it only once, but then the problem adapted and is now resistant to that. Evolution in action. :-) This affects different people on the team in different ways. Some don't have the problem as much, some try again and it works, etc. –  Jul 13 '11 at 19:21
  • Just tested fuslogvw.exe and it doesn't pick these sorts of things up. I suspect the loading through VS magic for the designer is going through a different path than for normal applications. –  Jul 13 '11 at 21:14
  • Does your `OurServiceBase` class have a parameterless constructor? It used to be the case that the designer could only work if it could instantiate an instance of the class and its base classes using a parameterless constructor. – adrianbanks Jul 15 '11 at 22:38
  • Good thought, I will check that when I get in on Monday – The Evil Greebo Jul 16 '11 at 09:57
  • It did have one, and I tried explicitly having it call base and not having it do so - no change. :( – The Evil Greebo Jul 19 '11 at 13:48
  • You might have to post some code from the EmailProcessor.cs file. I created some test classes like you described, but my designer keeps on working. – LarsTech Jul 19 '11 at 14:18
  • EmailProcessor.cs is empty at this point - its just a basic class file that's added when you add a WindowsService class. It looks exactly like "MyService" in all other respects. The only change I made is the change shown above to the base class. – The Evil Greebo Jul 19 '11 at 15:00
  • Can you post the code for OurServiceBase? Or try removing various members from there, until it works as expected. Since it looks like Visual Studio is having a problem serializing one of it's members. – CodeNaked Jul 19 '11 at 20:07
  • Bingo! I created a new project with a clean "OurServiceBase" service in it and derived MyService from it instead in its own project and the designer works, so now I can rebuild it bit by bit and find the offending code. If you will post that as an answer, I'll award the bounty. – The Evil Greebo Jul 19 '11 at 20:22
  • @Greebo - Done, and would still like to know what you find as the cause :-) – CodeNaked Jul 19 '11 at 20:32
  • @Greebo - Oh, and no need to award bounty right away. Make sure you get your issue resolved. You have 7 days! ;-) – CodeNaked Jul 19 '11 at 20:34
  • I seen this the yesterday in VS-2013. Closing and reopening worked. Glad I didn't get more complicated or spend much time at it. – Robert Koernke May 08 '19 at 10:15

19 Answers19

127

You can also try doing this:

  • Close all the UI design pages
  • Clean Solution
  • Build Solution
  • Open the desired UI design pages

This might or might not help but it certainly resolved the same issue in my project.

user2327795
  • 1,677
  • 4
  • 13
  • 16
  • 16
    Alternately, closing and restarting Visual Studio itself (not just closing & reopening your project) might fix your problem to. That's what ultimately fixed the issue for me after every other suggestion here failed. – ShadowLiberal Dec 06 '16 at 16:39
  • you guys are life saver, every time i encounter this, i just close and delete and create new project :/ – newbieguy Aug 12 '17 at 03:35
  • 8
    Closing all the designer windows and then closing Visual Studio works for me. Something about the designers gets hung up in VS and won't get straightened out until you release everything I guess. – Pete Feb 11 '19 at 21:56
  • Spot on... quite funny how the simplest approach solves the issue. – Vedran Mandić Jun 06 '19 at 09:21
  • 2
    Yes, resetting things helped here also ! I use VS2017 and my form contained some own graphics components I had not compiled for a long time. The above answer was too "light weight" for me.. I closed Visual Studio, removed the .vs directory of my solution and reopened Visual Studio and reloaded the solution. Then I did clean and rebuild solution, double clicked form design.. voilá – Goodies Sep 25 '19 at 07:11
  • I would like to notice, that you should close **ALL** Visual Studio instances, it helped in my case – Potato Dec 16 '19 at 07:09
  • 2
    This issue still occurs in VS 2019 and this solution still fixes the problem. – Tim Sep 18 '20 at 11:44
  • @Tim: One year later, that's still true. – Petter Hesselberg Nov 17 '21 at 21:18
  • 2
    @Tim: It's 2022 and this still happens in vs2022 – m_a_s Jan 12 '22 at 05:13
  • Not acceptable at the current tech pace – Naga Feb 11 '23 at 14:02
27

Here's another possible solution:

Under the project properties under build, my platform target was set to x64. I updated this to "Any CPU", rebuilt my project and the designers opened fine.

This explains it better: Visual studio designer in x64 doesn't work

Ausibanez
  • 279
  • 3
  • 4
  • 4
    OMG, this fixed my issue as well. At first, I was not convinced with the answer, but when implemented your suggestions it worked. Microsoft needs to fix this asap. – Bravo Jun 13 '19 at 08:51
12

Your best bet would be to start with a version of OurServiceBase with no functionality and see if you can design MyService. If so, then slowly add back functionality until it breaks.

Since it looks like Visual Studio is having a problem serializing one of the members of OurServiceBase.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • 1
    This is what got me to the solution. The shared service base and some of its dependencies were still in 3.5. When I ported the 3.5 code to 4.0 for all our shared libraries and pointed my service at the new code bases, the designer began working perfectly. – The Evil Greebo Jul 20 '11 at 13:57
  • 1
    I had this problem after creating a new local repository with the 4.7.1 framework. I changed version back to 4.7 and then 4.7.1 again and it worked again. – Mark Baijens Nov 12 '18 at 09:19
  • In my case, I accidently deleted a constructor for the base form class. As soon as I recovered the code having the constructor, everything worked fine. – Park JongBum Jul 25 '19 at 06:09
  • In my case (.NET 5 and regular form inheritance), a public NuGet package (contains a Framework DLL) is conflicting with the designer. When no reference - the designer works, when added - crashes. Had to comment and remove the package to design a new form. 8( – PIoneer_2 Nov 03 '21 at 11:37
10
  1. Open the "Designer.cs" file
  2. Cut all code and save the file
  3. Paste all code and save the file

Other steps involve:

  • Close and re-open the affected files.
  • Clean and rebuild project/solution
  • Restart Visual Studio

This will do the trick

5

Just in case some one like me have to work on a old project base on Visual Studio 2008 and face the same problem.

It's probably because the project path contain some exotic chars like C#

Example, my path look like that :

C:\projects\C#\projectname...

When it's renamed to

C:\projects\CSharp\projectname...

Visual Studio is now able to recognize parent class and then open the form with the inherited form.

Yanik Lupien
  • 120
  • 1
  • 5
  • I got it while trying to copy the contents of a file used in VS 2008 (Windows 7 desktop) to the same file in XPMode / VS 2003. Althought I think of the project as "%$#@?!?&^*!", there's really nothing exotic about the pathname or filename. – B. Clay Shannon-B. Crow Raven May 28 '14 at 16:24
  • Actually, all it takes in my case is to introduce one thing that is not recognized by the compiler (call a method that has not yet been added), and I get that err msg (along with the "[bla] does not contain a definition for 'blee'"). – B. Clay Shannon-B. Crow Raven May 28 '14 at 16:44
5

I had a solution with 2 projects (one referencing the other) and I had just set one to target .Net 4.5.2 and the other was targetting 4.5.

Tip: view the warning messages in the Error List:

There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "C:....dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
5

For me, with VS2022 Version 17.1.1 The fix was to right click an MDI tab and select Close All Tabs

Kirsten
  • 15,730
  • 41
  • 179
  • 318
4

I had this problem in a solution with several projects. I looked at all the projects compile tab, Target CPU. One project was set to x64 all the rest were AnyCPU. I made all the projects Target CPU = AnyCPU, Cleaned Solution, and Rebuilt Solution. Then I could view the form in designer mode.

Eric Moon
  • 101
  • 3
  • My "solution" was not exactly, what you wrote, but similar direction. I have created a template to a form and then added the template to the project,. After that, I had the problem. I had set "AnyCPU" to the (only one) project and had to change it to "X86" -> then, I was able to load the form in designer. After that, I have changed back to "AnyCPU". For me, this seems to be a bug in VS (2017) -> maybe especially related to template forms... – FredyWenger Jun 13 '23 at 14:52
3

You may also run into this problem if your control/service is inheriting from a generic class. The designer doesn't get along well with abstract classes in the hierarchy, since it has to instantiate them.

thomas88wp
  • 2,381
  • 19
  • 31
3

I just closed all the UI pages and cleaned my solution and build it again it started working.

soumya cb
  • 71
  • 1
2

You can also have this error if your form is in a shared project. The workaround is to exclude the file from the shared project, then create links to the file in the main projects.

Maxence
  • 12,868
  • 5
  • 57
  • 69
2

Just got the same issue using VS2019 (+ framework 4.7.2)

However, simply deleting the .vs hidden directory from the solution directory was enough. This folder is some sort of cache, and this files it contains can get corrupted and would need to be rebuild.

Note: this will delete all you breakpoints though

Go this hint from here.

... and also from @Goodies and @Tim (afterwards!) who also mentioned it in small print comments in this page. I worth posting it as an "Answer".

Christian
  • 88
  • 1
  • 6
1

I just had the same issue migrating from Visual Studio 2019 to Visual Studio 2022. All worked OK in 2019, designer failing in 2022.

In my case the projects were being build to target x86. Changed all projects with this setting to target Any CPU and that fixed it.

Jon Roberts
  • 2,262
  • 2
  • 13
  • 17
0
 form1.designer.cs // was showing this
           this.Components = new System.ComponentModel.Container();
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
   `enter code here`        this.ClientSize = new System.Drawing.Size(800, 450);
            this.Text = "form1";
  //  I had to add this to the code to get it to work
   this.Name = "form1";
   this.ResumeLayout(false);
   this.PerformLayout();
lee
  • 1
  • 2
0

Further to Ausibanez's answer above.

My app was in X64 but my problem was I had recently added a COM reference to WIA (Windows Image Acquisition) which was the issue.

But oddly...

  1. It worked for some time before it decided to no longer work, and
  2. WIA was not being called on the particular inherited form or its base.

But, once the reference to WIA was removed, all was OK again.

So, check your COM references!

Nathan Evans
  • 139
  • 2
  • 3
0
  1. Go to Project>>Properties>>Linker>>System, in the field "SubSystem" you choose "Windows (/SUBSYSTEM:WINDOWS)", click Apply.

  2. Go to Project>>Properties>>Linker>>Advanced, in the field "entry point", input value "Main" and Apply, finally click OK.

  3. Go to file code of form you created(ex: MyForm.cpp) and input code bellow to this file:

using namespace System;

using namespace System::Windows::Forms;

[STAThreadAttribute]

void Main(array<String^>^ args)

{

Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); Project1::MyForm form; Application::Run(%form);

}

  1. Save and rebuild.
Nico
  • 51
  • 4
0

My workaround was to remove the reference to System.Windows.Forms, and then add it back in again.

This is on a C++/CLI project, on .NET Framework 2.0.

John Go-Soco
  • 886
  • 1
  • 9
  • 20
0

Had a similar issue, exiting out and reloading visual studio seemed to fix the issue

0

I had this issue with a WinForms .NET framework project from 2016 that I developed with VS 2015. I opened it with VS 2022 and the error appeared. After downgrading the project from .NET 4.7.1 to 3.5, then back to 4.7.1, everything worked fine.

KennetsuR
  • 704
  • 8
  • 17
NotNameless
  • 41
  • 3
  • 9