Copying Over Forms in VS2022

Todd Lerew 60 Reputation points
2024-11-20T18:05:56.2533333+00:00

Hello everyone.

I am having an issue in VS2022 that I have never had before and do not know why.

I work for a company and I have created our ERP System in Visual Studio. We constantly add things or make changes to the programs, forms, reports, etc. There are three different people who work on this project, but I am the lead. We recently upgraded from VS2008 to VS2022, programming in Visual Basic. When one of us three makes changes to any windows forms, they copy the changed forms over to a shared folder that I copy to my project directory and empty the folder. I do all the updates/publishes on my copy of VS2022, so that data does not get overwritten. We have been doing this for a while with no issues. The last time I copied forms over and included them in my project, they did not come over as windows forms. When I include them in my project, in solution explorer, you can see all three files for a windows form, by the icon next to the form name in solution explorer isn't an form icon, it says "VB". If you click on it, all you see is the code. You cannot see "Form View". So I cannot add or edit any controls on the form itself. I have deleted the form, got an old one from a previous backup, and the same thing. This has never happened before - we have been copying and consolidating our project like this for years. Anyone have any idea why it is doing this? I can't even re-copy my original form back into my project, because it does the same thing.

I am very confused.

Thanks,

Todd

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,231 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,743 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 55,216 Reputation points
    2024-11-21T18:25:39.3166667+00:00

    FYI the latest VS2022 has broken some core features of copy-paste. The one issue I know has been confirmed fixed for the next update is copying/pasting files within Solution Explorer doesn't work for NET Framework projects. Not sure if you're using NF or Net Core.

    Someone piggy backed off that ticket to say they are having issues with VS not properly adding files with multiple subfiles. It sounds like the scenario you have. You can read more about it here.

    Personally I've never had much luck doing what you're doing. The problem is that VS sees the .cs file and assumes it is a regular C# file. So the file is added to the project as a source file and you don't get the "form" features you expect. Ultimately VS uses the designer type indicator to figure this out and if it is wrong then things don't work.

    You didn't clarify, but if you're building a Winforms app are you using the newer csproj format? If you are then the problem will likely just go away. In the csproj format the files are generally not listed at all. You literally just copy the files into your project directory and they auto-magically appear in the solution and will be compiled correctly. Winforms supports this format.

    You can get instructions here. AFAIK you can even do this with NET Framework projects by simply reverting the TargetFramework property after the migration. At least for non-web projects it works. The only issue you may run into is if you're still using package.config for Nuget references then you'd have to switch over to the package references being in the project file. This breaks any legacy Nuget package that requires running a transform when it is updated. Fortunately most of these aren't around anymore. It is also possible the Upgrade Assistant extension in VS can do this for you, haven't tried it myself.

    If you need to stick with the verbose project file format then you need to change the form's .cs file to a Form subtype. That means editing the project file by hand. Something like this.

    <ItemGroup>
       <Compile Include="MyForm.cs">
          <SubType>Form</SubType>
       </Compile>
       <Compile Include="MyForm.designer.cs">
          <DependentUpon>MyForm.cs</DependentUpon>
       </Compile>
    </ItemGroup>
    

    Basically you add the subtype and VS will now recognize it as a form. The second include is to get the designer file to nest under the main form. After that reload the project and the problem is solved.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.