Slateboard
Slateboard

Reputation: 1031

Is there a way to convert my Console Application into a Windows Forms Application in C#?

I created a console application, but I want to turn it into a windows forms application.

I found This and it appeared to be what I needed, but I got an error message when I tried to use using System.Windows.Forms;

This is the error message I got:

Error 1 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)

Is there another step, or is it somehow different in VS 2008?

Upvotes: 6

Views: 11255

Answers (4)

Pavel Sapehin
Pavel Sapehin

Reputation: 1078

The easiest way:

Starting with .Net Core 3 the easiest way to do it is to backup your Program.cs somewhere on a disk (or just using git) and run the following command where Program.cs is located:

dotnet new winforms --force

It will replace your Program.cs and .csproj. Then just copy required code from your old Program.cs. That's it!

For manually converting the project this may be helpful:

Here is how .Net Core 3 project looks like (generated using dotnet new winforms):

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

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

Here is how Program.cs looks for a new project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LinkInterceptor
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Upvotes: 1

Bryan Rowe
Bryan Rowe

Reputation: 9303

Make sure you add the System.Windows.Forms assembly in your references for the project. In the solution explorer, right click on 'References' and then under the .NET tab find the System.Windows.Forms assembly and add it.

Upvotes: 3

JaredPar
JaredPar

Reputation: 754545

You need to add a reference to the WinForms assembly

  • Right click on the solution and select "Add Reference"
  • Select System.Windows.Forms and hit OK

You may need to do the same for System.Data as well depending on your project setup

Upvotes: 13

Jo&#227;o Angelo
Jo&#227;o Angelo

Reputation: 57658

You need to add a reference to System.Windows.Forms. Right-click your project and choose Add Reference.

On the .NET tab choose the the previously mentioned reference.

Upvotes: 2

Related Questions