Vishnu Pradeep
Vishnu Pradeep

Reputation: 2097

how to change a c# console project to windows forms application project?

created a c# console project, which contains a console class (program.cs). i added a form (Main.cs) to the project.

I want to do one of the below things with my project
1. Change the project to a Windows Form Application,can i change it to a Windows Form Application without creating a new project.
2. Set Main class as the startup object. Checked 'Application' tab in the properties window. The main class it not in the Startup object list.

Upvotes: 3

Views: 3897

Answers (4)

Simon Fischer
Simon Fischer

Reputation: 3876

alt text

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941465

Things you have to do:

  1. Project + Properties, Application tab, change Output Type to "Windows Application"
  2. Project + Add Reference, select at least System.Windows.Forms and System.Drawing
  3. Locate your Main() method in the Program.cs source code file. Give it the [STAThread] attribute.
  4. Rewrite the Main() method to look like this:

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new YourMainForm());
    }
    

Change "YourMainForm" to the name of your form class. Salvaging your existing code in the Main() method is what will probably be the painful bit. You cannot leave it after the Run() call, that call won't return until the user closes your main form.

Upvotes: 7

Rotsor
Rotsor

Reputation: 13783

  1. Change the project to a Windows Form Application Go to project properties -> "Application" tab -> "Output type" combo-box, and select "Windows Application".

  2. Set Main class as the startup object You don't have to set a startup object as long as you have only a single class containing a static method Main(). To run a windows application you have to implement a method Main containing a call to Application.Run(). You can create a windows forms project with a new project wizard to see an example.

Upvotes: 2

stuartd
stuartd

Reputation: 73253

In the project properties, change the Output Type from Console Application to Windows Application.

Upvotes: 3

Related Questions