简体   繁体   中英

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

created ac# 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.

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.

在项目属性中,将输出类型从Console Application更改为Windows Application

  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.

替代文字

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM