2

I have a console application and I just want to display a MessageBox at some point.

I found a page that stated that I could do it by adding a reference to the assembly. However, just adding using System.Windows.Forms doesn't work (CS02348: it doesn't exist in the namespace and I'm probably missing an assembly reference).

However, I can only seem to add COM references to my project. When I looked for a way to display the assembly panel, I found this page that seems to state that I should already have it.

According to this tutorial, I should manually browse and seek in C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App.

I tried with C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\7.0.2\System.Windows.Forms.dll and C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.13\System.Windows.Forms.dll. With both of them, I get the error CS1705:

Error CS1705 Assembly 'System.Windows.Forms' with identity 'System.Windows.Forms, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' uses 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Runtime' with identity 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

I just deleted the System.Windows.Forms.dll I manually imported and my *.csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="RandomLibrary">
      <HintPath>RandomLibrary.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

I need it to stay in 6.0 for the LTS.

Why don't I have this assembly tab? How to add reference to System.Windows.Forms? Why did I get the same message with version 6.0.13 than with version 7.0.2?

Nocxy
  • 111
  • 7
  • 1
    Does this answer your question? [How do I show a console output/window in a forms application?](https://stackoverflow.com/questions/4362111/how-do-i-show-a-console-output-window-in-a-forms-application) – Tu deschizi eu inchid Apr 27 '23 at 23:53
  • I asked the question because it is possible. I don't even get why you got an upvote since your comment came after an answer proving that you can do it. For your new comment: no, it doesn't but the answer below does. However, if you have answers to my other questions, I'll happily read them. – Nocxy Apr 27 '23 at 23:53
  • 1
    _Why don't I have this assembly tab? How to add reference to System.Windows.Forms?_ : The following may be of interest: [dotnet restore](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-restore#description) – Tu deschizi eu inchid Apr 28 '23 at 00:08

1 Answers1

3

You don't have to add anything in your Dependencies or manually reference any dlls. You just have to add a few lines to your csproj file.

Here is a fresh one generated by the console app wizard from Visual Studio:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

If you want to use win forms, modify it thus (right click on the project, select 'Unload project' or just edit it manually in a text editor):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWindowsForms>true</UseWindowsForms>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Note the addition of -windows after net6.0 as well as <UseWindowsForms>true</UseWindowsForms>.

Then you can use win forms and see console output:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
string message = "Hello,";
string caption = "World!";

MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;

// Displays the MessageBox.
result = MessageBox.Show(message, caption, buttons);

enter image description here

Alternatively, you can do as the answers from user09938's link suggest and create a WinForms project, then go into properties and change the first box (output type) from Windows Application to Console Application.

dmedine
  • 1,430
  • 8
  • 25
  • Thank you for your fast answer. Do you know why VS doesn't display an ``assemblies`` tab and why I got the exact same error message with both ``System.Windows.Forms`` versions? – Nocxy Apr 27 '23 at 23:50
  • 1
    Honestly, no. But I do know that if you want to play with .NET, you have to play by their rules. Visual Studio provides you with a bunch of wizards that are meant to help you construct a csproj file 'the right way'. Sometimes, you have to edit them manually in order to do hacky things like this. The assemblies tag seems to have gone out between .NET Framework 4.8 and .NET Core. These iterations of .NET have drastically different csproj file structures and I imagine that is the reason for the change in VS. – dmedine Apr 28 '23 at 00:00
  • I already found someone doing the same thing but it was a few weeks ago when I didn't need it. I created another project to spot the differences but focused on which files were present or not. – Nocxy Apr 28 '23 at 00:08