Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a .NET console application to a Winforms or WPF application

I frequently start with a simple console application to try out an idea, then create a new GUI based project and copy the code in. Is there a better way? Can I convert my existing console application easily?

like image 705
Jon Galloway Avatar asked Sep 27 '08 23:09

Jon Galloway


People also ask

What is the difference between Windows Forms application and console 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.

How do I convert WinForms to .NET core?

Preview WinForms designer in Visual Studio. To enable the designer, go to Tools > Options > Environment > Preview Features and select the Use the preview Windows Forms designer for . NET Core apps option.


2 Answers

Just add a new Winform, add the following code to your Main:

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());

Also, be sure the [STAThread] attribute is declared above your Main function to indicate the COM threading model your Windows application will use (more about STAThread here).

Then right click your project and select properties and change the "Output type" to Windows application and you're done.

EDIT :

In VS2008 the property to change is Application type

enter image description here

like image 136
albertein Avatar answered Oct 13 '22 07:10

albertein


For completeness - and for other newbs like me - you also need to add:

using System.Windows.Forms;

... to the top of Program.cs

like image 34
dbruning Avatar answered Oct 13 '22 07:10

dbruning