Compiler cannot find System.Windows.Forms namespace

Robson, James 61 Reputation points
2024-01-05T21:46:43.0666667+00:00

I'm writing a console app in C#, targeting .Net 8.0. I want to pop a MessageBox, but the compiler can't find System.Windows.Forms. Here's a screenshot of the error:
WindowsFormsError

I've searched but cannot find an assembly or package that fixes this issue. Any ideas?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,562 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,551 questions
{count} votes

Accepted answer
  1. Viorel 114K Reputation points
    2024-01-05T22:00:08.0033333+00:00

    Try to double-click the project in Solution Explorer, and enter the <UseWindowsForms> element:

    <Project Sdk="Microsoft.NET.Sdk">
    
        <PropertyGroup>
            . . .
            <UseWindowsForms>true</UseWindowsForms>
        </PropertyGroup>
       . . .
    

    Save it. Then right-click the project, go to Property, and select “Windows” in Target OS field.

    3 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,291 Reputation points
    2024-01-05T22:43:30.7533333+00:00

    You can create a Windows Forms project, under project properties change Output type to Console Application. Then in Program.cs

    namespace WinFormsApp2;
    
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            MessageBox.Show("Test");
        }
    }
    

    Or

    namespace WinFormsApp2;
    
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            MessageBox.Show("Test");
        }
    }
    
    0 comments No comments