controls and forms missing icon and view designer option in vs2019 running SDK project with framework 4.7.2
Asked Answered
C

3

8

Summary answer so far: Forms and controls only open in the designer if csproj.user file exists.

The problem story:

I have 2 controls that both inherit from a base control. However one is missing the control icon in the solution explorer missing icon

The good control source starts with

public partial class UniTabMaterialsControl : UnitabBaseControl
{
    public UniTabMaterialsControl()
    {
        InitializeComponent();
    }

The bad control source starts with

public partial class UniTabMaterialsControl : UnitabBaseControl
{
     
    public UniTabMaterialsControl()
    {
        InitializeComponent();
    }

The base control class is

public class UnitabBaseControl : UserControl
{
}

I have tried closing and re-opening Visual Studio

[Update]

I added a new user control and set it to also inherit from UnitabBaseControl , strangely the behaviour corrected.

I was then able to delete the new control and maintain correct behaviour

Git shows that there has been a change in UnitabBaseControl.cs but I do not see what changed other than the colour of the UserControl text

base class colour changed

I then made a fresh clone of the whole solution. This time the incorrect behaviour showed on all the controls that inherited from UnitabBaseControl

[Update]

Now I suffer the same issue with forms

form not showing correct icon

using System.Windows.Forms;

namespace SBD.VivSnap.UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

and

using System.Windows.Forms;

namespace SBD.VivSnap.UI
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    }
}

[Update]

I neglected to mention that my project was a library project referred to by the start up project.

I deleted and then re-added the library project reference in the start up project. Then all the forms and controls appeared correctly.

I could not find any changes in Git to indicate why the solution now worked.

Since then I have noticed the problem in my main project

problem in main project

[Update]

It turns out that my project is in .sdk format even though it is framework 4.7.2 I am thinking this may have something to do with it.

I am using Framework 4.7.2 with an sdk project

Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"

I checked out the code onto a different computer running VS2019 16.1.4 and it builds ok.

I am using 16.11.4

[Update] Now I have updated to 16.11.5 and cloned again but still have the problem.

[Update] The project file is

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <OutputType>WinExe</OutputType>
    <AssemblyName>Main</AssemblyName>
    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
    <RestorePackages>true</RestorePackages>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <PropertyGroup>
    <StartupObject>SBD.VivSnap.Main.Program</StartupObject>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="Microsoft.Data.SqlClient">
      <Version>2.0.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore">
      <Version>3.1.12</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer">
      <Version>3.1.12</Version>
    </PackageReference>
    <PackageReference Include="NuGet.Build.Tasks.Pack" Version="5.7.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\SnapInUI\SBD.VivSnap.UI.csproj" />
  </ItemGroup>
  <ItemGroup>
    <Compile Update="Properties\Settings.Designer.cs">
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <None Update="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>
</Project>

The csproj.user file is in my answer.

Calle answered 27/9, 2021 at 21:34 Comment(0)
C
6

The form's compile type is probably not set properly as "form" in the .csproj file. Open the .csproj file and make sure you have this line of code wherever "form1.cs" is mentioned:

<Compile Include="form1.cs">
      <SubType>Form</SubType>
    </Compile>
Clockmaker answered 13/10, 2021 at 14:9 Comment(3)
My apologies I neglected to mention that I am using a Sdk format for the project. I updated the question.Calle
This is on the right track. I was able to copy from csproj.user to csproj and the designers now work. Now to fix the controls in a releated project.Calle
I was able to fix similarly, the controls in other projects using <SubType>UserControl</SubType> Goodness knows how the project files got corrupt.Calle
C
1

It turns out that the csproj.user files were not in source control. When I copied myproject.csproj.user to the project folder and opened in VS then the forms appeared correctly.

The file contents is

<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup />
  <ItemGroup>
    <Compile Update="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Update="Form2.cs">
      <SubType>Form</SubType>
    </Compile>
  </ItemGroup>
</Project>

This leaves me with the dilemma of whether or not to check the csproj.user into source control. I had thought this was not necessary before but it seems it is. Plus the name "user" would imply it is user specific.

Calle answered 19/10, 2021 at 4:31 Comment(1)
As suggested by the other answer, I also think this should be in the normal .csproj. Committing the .csproj.user is a bad idea, since it (eventually) contains user-specific debugging information (such as the parameters used or the absolute paths to the startup file).Lobito
H
1

UPDATE (fixed in VS2022 update)

As of 2024-05-02, this has been fixed. Update to the latest version of VS2022.

Solution for older versions

This solution uses globs (pattern matching) for maintainability. No need to include EVERY form file.

  • Create a "Forms" folder for all your forms

  • Rename form files to end with "Form"

  • Use glob matching to find these files (and apply <SubType>Form</SubType>)

    <Project Sdk="Microsoft.NET.Sdk">
      <ItemGroup Label="Make Winforms Designer work">
        <!-- Mark startup page as a form -->
        <Compile Update="MainForm.vb">
          <SubType>Form</SubType>
        </Compile>
        <!-- Mark all other forms in "Forms" folder as a form
             NOTE: File name must also end with "Form.cs"
        -->
        <Compile Update="Forms\**\*Form.cs">
          <SubType>Form</SubType>
        </Compile>
    </ItemGroup>
    
Haberdasher answered 24/3, 2023 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.