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

1k views Asked by At

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.

3

There are 3 answers

3
user17143414 On BEST ANSWER

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>
1
Kirsten On

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.

0
Nick4814 On

Overview

This solution uses globs (pattern matching) for maintainability.

  • No need to include EVERY form file.

Problem

  • SDK projects are currently broken for windows forms (as of 2023-03-24)
  • The only way (that I have found) is to manually edit the project file (.vbproj in my case)

Solution

  • 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>)
  • See project file below

Project file

<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
            - File name must also end with "Form.vb"
        -->
        <Compile Update="Forms\**\*Form.vb">
            <SubType>Form</SubType>
        </Compile>
    </ItemGroup>
    
    ...
</Project>