question

BrandonStewart-4522 avatar image
0 Votes"
BrandonStewart-4522 asked BrandonStewart-4522 commented

Make it easier to reference WinForms and other libraries in .NET 5/Core console and library projects

Would the Visual Basic development team at Microsoft please, please, please make it easier to reference WinForms (and other libraries) when creating .NET 5/Core console program and DLL library projects? I very often need to use message boxes in both types of projects, and other user forms within libraries. Currently, we must manually type in the reference within the MyApp.vbproj project file because WinForms is not available in the project references tab. Yes, I know that console apps and WinForms apps are two separate entities, but it is very convenient to be able to mix the two.

windows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

remember .net 5 is cross platform. WindowsForms is windows only but console apps will run on windows, mac, and linux.

0 Votes 0 ·

Correct. However, at the same time Microsoft is also pushing all .NET developers (including Windows only) away from the Framework and toward 5/Core, which is the only reason I updated all my code to .NET 5. Since they are pushing so hard for the change, the least they could do is make it easier for Windows only folks like myself to reference WinForms in .NET 5/Core.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered BrandonStewart-4522 commented

Hello,

A decent alternative to what you are doing now is


  1. Create a new project e.g. class project

  2. Alter the project file as you are doing now

  3. From Visual Studio's menu select Project,

  4. Select Export Template

  5. Follow the prompts to export a project

  6. Select the project

  7. Fill in the default name for a project

  8. Fill in a description

  9. Finish up

  10. Restart Visual Studio

  11. Type in the name of the template e.g. my template for a class project for .NET 5 is PayneClassProject

  12. Give a name for the project, done,

Once done a folder opens, if you need to share the template this is where other developers place the .zip file

84483-f1.png



f1.png (21.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, Karen. Very helpful as always. A template is just as good as a checkbox for what I need..

0 Votes 0 ·
vb2ae avatar image
0 Votes"
vb2ae answered lextm edited

Try this. Change the TFM (Target Framework Monitor) from .net 5 to .net-windows in the console apps project file and add UseWindowsForms

 <Project Sdk="Microsoft.NET.Sdk">
    
     <PropertyGroup>
        <OutputType>Exe</OutputType>
       <TargetFramework>net5.0-windows</TargetFramework>
       <UseWindowsForms>true</UseWindowsForms>
    </PropertyGroup>
  </Project>


Then you could try something like this in your code

 using System;
 using System.Threading.Tasks;
 using System.Windows.Forms;

 namespace WindowsFormsInConsole
 {
      class Program
     {
           static void Main(string[] args)
          {
                 Console.WriteLine("Hello World!");
                MessageBox.Show("Test");
          }
     }
 }


Sample on GitHub


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is the process which I currently use. However, the purpose of this post is to get Microsoft to include adding a Winforms reference by simply checking a box in order to bypass having to manualy type in the reference, similar to how we can currently do in a .NET Framework based application

0 Votes 0 ·
lextm avatar image lextm BrandonStewart-4522 ·

What you preferred was well discussed (many GitHub issues, and this is just one of them) when they designed .NET 5 experience, and the conclusion was to use the approach described by @vb2ae . You can dislike that but what's done is done.

0 Votes 0 ·