Compiler cannot find System.Windows.Forms namespace

Robson, James 66 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?

Developer technologies .NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Viorel 122.6K 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.

    6 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    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

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.