charilaos13
charilaos13

Reputation: 543

.NET C# main method and windows forms

I have a Visual Studio project that uses C# and it is probably a Console Application. When I try to run/Build/Debug the Project it look's for the Main method in an existing class. I have added a Windows form on that Project and I want it to run in the Windows Form version and not in the command line (expecting arguments). Can you tell me how to edit the run time of the project to look for the Windows Forms instead of the static void main()?

Upvotes: 1

Views: 14747

Answers (4)

Russell Keane
Russell Keane

Reputation: 547

Method 1

In the main function use the following:

Application.Run(new Form1());

You will also need to add the following line at the top of your file:

using System.Windows.Forms;


Method 2

In the main function, you could add this:

Form1 c = new Form1();
c.ShowDialog();


Both methods will show your form as a dialog. The console will still be visible in the background however.

If you want to then hide the console window the following link gives instructions to do so (but it's a little convoluted):

http://social.msdn.microsoft.com/Forums/vstudio/en-US/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd/hide-console-window-in-c-console-application

Upvotes: 2

Ankush Madankar
Ankush Madankar

Reputation: 3834

Change ur Programe.cs file to

  class Program
  {
    [STAThread]
    static void Main()
    {
        //
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //
        Application.Run(new Form1());

    }
  }

And then right click on Console project and go to properties and Set Output Type as Windows Application

Upvotes: 2

user1968030
user1968030

Reputation:

You can change type project

Small correction: Visual Studio does not keep track of the project template used to create a project. The project system is largely unaware of the initial template used for a project. There are several items in the project system (Project Type for instance) which have the same name as specific templates but this is a coincidence and the two are not definitively corrected.

The only thing that can really be changed in terms of the project type is essentially the output type. This can have value Class

Library, Console Application and Windows Application. You can change this by going to the project property page (right click Properties) and change the Output Type combo box.

It is possible to have other project types supported by the project system but they are fairly few and are not definitively associated with a project template.

enter image description here

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166356

When creating a new Console application, the default behavior is to add a class named Program with a method called Main

Something like this.

class Program
{
    static void Main(string[] args)
    {
    }
}

Whereas the default for a Windows application is

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

Related Questions