Visual Studio .NET error: Form does not contain Di

2019-06-08 10:10发布

问题:

Occasionally Visual Studio (2005, 2008, 2010) will lose its mind, and get confused by a WinForm:

SplashForm.cs:

public partial class SplashForm : Form
{
   ...

SplashForm.Designer.cs:

partial class SplashForm
{
   ...
   protected override void Dispose(bool disposing)
   {
      if (disposing && (components != null))
      {
         components.Dispose();
      }
      base.Dispose(disposing);
   }
   ...

It will complain:

  • protected override void Dispose(bool disposing)
    'Dispose(bool)' has no suitable method to override
  • base.Dispose(disposing);
    'object' does not contain a definition for 'Dispose`

It's complaining about code that it generated.

It's been too long (3 years) since i dealt with Visual Studio, so i've forgotten the trick to get Visual Studio's head out of its own assembly.

What's the trick to get it going again?

回答1:

I had this issue today and found this question. I managed to fix it and while the post is old (2 years old now) I think it is always good to answer it with the solution I found:

  • My Scenario: I created a form in the wrong directory
  • The result: The namespace was different from the namespaces of the other forms.
  • The cause: In my specific case, this was caused because I changed the <Form>.Designer.cs namespace so that it matched the rest of the forms Namespace. But I didn't change the <Form>.cs namespace and that was my mistake.

So, if you're having this error, make sure both your namespaces from the <Form>.Designer.cs and <Form>.cs match.



回答2:

Well i fixed it.

i excluded:

  • SplashForm.cs
  • SplashForm.Designer.cs
  • SplashForm.resx

from the project, and moved the files to another folder.

Then i created a new form, called SplashForm, and copied the contents of:

  • SplashForm.cs
  • SplashForm.Designer.cs
  • SplashForm.resx

from the backup copies.