views:

861

answers:

2

Hi, when I try to click on the designer tab to get the designer view, I get this error:

To prevent possible data loss before loading the designer, the following errors must be resolved:
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: Form1 --- The base class 'System.Windows.Forms.Form' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

I'd like to get into the designer and have no idea what's wrong. My Form1 code looks like this at the top:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Diagnostics;

    namespace foobar
    {
        public partial class Form1 : Form
        {

        List<CharLabel> allChars = new List<CharLabel>();

        public Form1()
        {
... etc ...

Any ideas?

+1  A: 

Have you checked the recomendation in the message? That is, have you verified that System.Windows.Forms.dll is referenced in your project?

To add the reference if it's missing do the following

  • Click: View -> Solution Explorer
  • Right Click on the References node and select "Add Referenc"
  • Go to the .Net Tab
  • Scroll until you see System.Windows.Forms.dll
  • Select that and hit OK
JaredPar
I have not (and the error makes no mention of the ".dll" part so I didn't know to look at that). How would I make that reference?The thing about this is that it just suddenly starts giving the error, then a restart may fix it for a bit, etc.Thanks for the help.
cksubs
@cksubs, I just added steps to get this added to your project.
JaredPar
I guess I should have checked to see if it was there first, but I just added the file via your steps, and it shows up in the tree view under References in the Solution Explorer, but I still get the same error and am unable to open the designer.Any other suggestions? Blah.
cksubs
@cksubs, try restarting VS at this point. If that doesn't work can you list the DLL's starting with System which are referenced?
JaredPar
Sure, just restarted. I see about 1.5 seconds of my designer form before it is replaced with the error page.From the solutions explorer tree:SystemSystem.DataSystem.DeploymentSystem.DesignSystem.DrawingSystem.Windows.FormsSystem.XmlAnd then also the ones from my code in the original question if those matter
cksubs
Did you change the namespace, class name or ui components names directly from the code view?
Freddy
I was given two .cs files, and I switched their namespaces to match my code's. My impression is that that is the only way to allow my code to access them. I don't think I made any other changes to the items you listed.
cksubs
+2  A: 

When you change the namespace or other item inside a partial class (like Forms) directly from the code editor you are making an invitation for madness. As the name suggest a partial class is a class that is defined "partially" on the code view, but there is another part which is generated automaticall by VS and that is the other part of the class. In that part it contains the definition of all UI elements, fonts, default values, etc. When you change the name space in one part of the class the other part don't know what do and then the interesting errors start. When changing namespaces, class names, event methods names always use the Refactor option in VS.

In your case, I would probably go back to the old name it had, and then use Refactor option VS provides (Highlight the component name, Ricgh click, refactor->rename)

Hope this help.

Freddy
Thanks a a lot.Completely deleting the files, opening the original copies up and using the Refactor thing to set the correct new namespace, and then restarting Visual Studio worked great.
cksubs