Home > other >  Unable to copy-paste Winforms controls from a form in a vb.net project to a form in a c# project
Unable to copy-paste Winforms controls from a form in a vb.net project to a form in a c# project

Time:01-18

for a big refactoring project of about 140 forms I've been trying to copy and paste these controls through the designer. I'm using VS 2022 but I've also tried with 2019. The origin project is in .NET4.5 and the new project is in dotnet6. My machine is running on win10.

These are the things I've tried so far:

  • keyboard shortcuts ( ctrl-a ctrl-c / ctrl-x ctrl-v )
  • Drag 'n drop ( ctrl-a drag drop )
  • Same methods as above but one control at a time ( total size of clip is 164k bytes )

enter image description here

The paste option in the context menu of the c# form is grayed out and when I drag 'n drop I'm shown a "blocking" icon.

I'm at a loss and not about to redesign all these forms.

Am I missing something here?

CodePudding user response:

I just did some testing and it appears that you cannot drag and drop - or copy and paste, presumably - between different instances of VS, which you appear to be trying to do. I opened the same solution twice and I was able to drag and drop between C# and VB projects in the same instance but I wasn't able to do so between two forms in the same VB project between instances.

This means that you will have to add one of the projects to the solution containing the other. That's not a big deal because adding an existing project to a solution doesn't move or copy that project. The solution simply refers to it in its existing location. Once you've done all the drag and drop, you can simply remove the project from the solution again and you're done.

CodePudding user response:

According to this post, .NET framework 4.x is incompatible at the binary level with .NET core which prevents you from copying over controls. Logical really when I come to think about it.

I've tested with dotnet5.0 and dotnet6.0 both giving issues but dotnet4.8 works just fine.

As a solution to my problem I will try using an intermediate project in .NET framework 4.8 using c# and afterwards porting to dotnet6.

Thanks for the help.

CodePudding user response:

You cannot directly copy control from .NET Framework 4.x to .NET6 in winform application, because .NET Framework and .NET6 are different frameworks, so if your project only targets windows, you can consider updating the project to .NET Framework 4.8 which allows you to copy-paste Winforms controls.

In order to convert VB project(.NET Framework) to C# project(.NET6), one of the option is to convert VB code in the InitializeComponent method to C#, and paste it into the same location in .NET 6.

Also check : Upgrade a Windows Forms App to .NET 6 with the .NET Upgrade Assistant

Note: some libraries in .NET Framework are not supported on .NET6, you may need to find their alternatives.

  •  Tags:  
  • Related