Visual Studio Design View - form is blank

后端 未结 6 1079
别跟我提以往
别跟我提以往 2021-02-04 14:38

I have a C# project with two forms. All compiles well. When I run the project, all forms are drawn as they are expected to.

A couple of days back, the DesignView of

相关标签:
6条回答
  • 2021-02-04 15:13

    Another possibility: The designer seems to try to design the first class in the .h file, no matter what's its name. I just had an empty forward ref class declaration before the actual form class, and it tried use that.

    0 讨论(0)
  • 2021-02-04 15:19

    Another possibility: I had a super-class of Form from which all of my forms derived. I had defined this super-class in one of my form.cs files, above the (partial) class definition for the form corresponding to that file. This seems to have confused visual studio and it was displaying the contents of my super-class (i.e., an empty form) instead of the contents of the form corresponding to the file. The solution was to move the definition of the super-class to a different file that wasn't a form-designer file.

    0 讨论(0)
  • 2021-02-04 15:22

    Another possibility. I had a group of forms all descended from a base form. I added location save/restore code--and everything was coming up blank.

    After an hour of messing with it and not being able to find anything wrong it suddenly started throwing a null reference exception in the designer rather than just being blank. Duh--the save/restore code used an object that didn't exist design time.

    Why it was silent about it and then suddenly started squawking I have no idea.

    0 讨论(0)
  • 2021-02-04 15:25

    I solved the problem by removing all files associated with that form

    • File -> Right-click -> Exclude from Project

    and adding the files manually back to the project

    • Project -> Add -> Existing Item...
    0 讨论(0)
  • 2021-02-04 15:28

    For those searching around and finding no solace, I have another solution. The project I inherited has horrible code quality and the previous developer had mixed case all over his code because "It's case insensitive, so it doesn't matter"

    So, while PopulateRevs is, in fact, identical to POpulateREvs in Visual Basic, that kind of laziness leads to issues. To wit:

    <Compile Include="Inventory\Partmaster.Designer.Vb">
      <DependentUpon>Partmaster.vb</DependentUpon>
    </Compile>
    <Compile Include="Inventory\Partmaster.vb">
      <SubType>Form</SubType>
    </Compile>
    

    I happened to see this in the .vbproj file while looking at other solutions that didn't work. I happened to change Partmaster.Designer.Vb to Partmaster.Designer.vb (lowercase extension) and it worked.

    So as another solution, while Visual Basic is not case sensitive, it looks like the .vbproj file is. If you're stumped, another thing to make sure is that your file names and .proj files share the same case.

    (I realize this is tagged C#, but I tested this in C# and the mixed case will cause problems there as well.)

    0 讨论(0)
  • 2021-02-04 15:29

    Your project file has become invalid.

    A valid project entry for a form looks like this:

    <Compile Include="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Form1.Designer.cs">
      <DependentUpon>Form1.cs</DependentUpon>
    </Compile>
    

    Yours though is missing the DependentUpon line - which is why the code and designer files appear separately in the project, instead of being connected:

    <Compile Include="mainForm.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="mainForm.Designer.cs" />
    

    If you add in the missing line, the form displays correctly in design mode:

    <Compile Include="mainForm.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="mainForm.Designer.cs">
      <DependentUpon>mainForm.cs</DependentUpon>
    </Compile>
    

    And to tidy up the resource file:

    <EmbeddedResource Include="mainForm.resx">
      <DependentUpon>mainform.cs</DependentUpon>
    </EmbeddedResource>
    

    In order to fix this, you can just edit the csproj in an editor, or to do it in Visual Studio:

    1. Back up the files
    2. Right-click the project and choose "Unload Project"
    3. Right-click the unloaded project and choose "Edit [ProjectName].csproj"
    4. Make your changes, and close the file.
    5. Right-click the unloaded project and choose "Reload Project"
    0 讨论(0)
提交回复
热议问题