Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 576
Slateboard Avatar asked Mar 03 '10 18:03

Slateboard


People also ask

What is the difference between console application and Windows form application?

A Windows form application is an application that has a graphical user interface(GUI) like the Visual C# IDE. A console program on the other hand is a text application. There are not fancy controls like buttons or textboxes in a console application and they are run from the command prompt.


4 Answers

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

like image 195
JaredPar Avatar answered Nov 11 '22 12:11

JaredPar


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.

like image 43
Bryan Rowe Avatar answered Nov 11 '22 11:11

Bryan Rowe


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.

like image 43
João Angelo Avatar answered Nov 11 '22 12:11

João Angelo


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());
        }
    }
}
like image 20
Pavel Sapehin Avatar answered Nov 11 '22 11:11

Pavel Sapehin