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()
?

- 543
- 2
- 7
- 19
-
Maybe it's a stupid question, but did you declared the Windows Form project as "Starting Project" ? – Santanor Aug 08 '13 at 09:53
-
You can change the startup object? (project properties) – Sayse Aug 08 '13 at 09:54
-
Your windowsForms App `Main` method should be there, and Set your windowsForms App as `StartUpProject` – Sriram Sakthivel Aug 08 '13 at 09:55
4 Answers
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):

- 537
- 1
- 6
- 12
-
-
-
Although it worked (and thank you a million for your help) the form takes at least 5 secs to show up do you know why? – charilaos13 Aug 08 '13 at 12:06
-
It depends what you're doing in the form load and in the main function before you call the form load. If there's nothing in it then it should be pretty quick. – Russell Keane Aug 08 '13 at 12:09
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

- 3,689
- 4
- 40
- 74
-
When I tried your suggestion I got:`The name Application does not exist in the current context` – charilaos13 Aug 08 '13 at 11:27
-
-
When U add new Windows Form in ur project then automaticaly required DLL get added in project references, use those. – Ankush Madankar Aug 08 '13 at 11:50
-
what does it mean when I click run for a project and it builds successfully and but does not show anything. – Doug Hauf Feb 28 '14 at 14:47
-
@Doug Hauf that mens you are missing Application.Run(new Form1()); in ur program.cs file.. Add one windows form in ur project and initilize new constructor of that form from ur program.cs file – Ankush Madankar Mar 02 '14 at 10:51
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());
}
}

- 162,879
- 31
- 289
- 284
-
-
By doing this, Yes we can run `Form1` but how to hide `Console window` which runing at backgroud? – Ankush Madankar Aug 08 '13 at 10:12
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.

- 1
- 1
-
I already did this but still when I press F5 it looks for the Main Method. I need it to start in my form and not the MAIN(). – charilaos13 Aug 08 '13 at 10:10
-
-
So you are saying that the only way to make the app run from my Added Windows Forms is by calling them through the MAIN Method? – charilaos13 Aug 08 '13 at 10:17