1

我有一个使用的 Visual Studio 项目,C#它可能是一个Console Application. 当我尝试运行/构建/调试项目时,它会查找现有类中的 Main 方法。我在该项目上添加了一个 Windows 窗体,我希望它在 Windows 窗体版本中运行,而不是在命令行中(期望参数)。你能告诉我如何编辑项目的运行时间来寻找 Windows 窗体而不是static void main()?

4

4 回答 4

2

方法一

在主函数中使用以下内容:

Application.Run(new Form1());

您还需要在文件顶部添加以下行:

using System.Windows.Forms;


方法二

在 main 函数中,您可以添加以下内容:

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


这两种方法都会将您的表单显示为对话框。但是,控制台仍将在后台可见。

如果你想隐藏控制台窗口,下面的链接给出了这样做的说明(但它有点令人费解):

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

于 2013-08-08T09:54:01.143 回答
2

将您的 Programe.cs 文件更改为

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

    }
  }

然后右键单击Console project并转到properties并设置Output Type为Windows应用程序

于 2013-08-08T10:18:03.940 回答
1

创建新的控制台应用程序时,默认行为是添加一个Program名为 Main的类

像这样的东西。

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

而 Windows 应用程序的默认设置是

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());
    }
}
于 2013-08-08T09:54:44.137 回答
1

您可以更改类型项目

小修正:Visual Studio 不跟踪用于创建项目的项目模板。项目系统很大程度上不知道用于项目的初始模板。项目系统中有几个项目(例如项目类型)与特定模板具有相同的名称,但这是一个巧合,这两个项目并没有得到明确的纠正。

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

库、控制台应用程序和 Windows 应用程序。您可以通过转到项目属性页面(右键单击属性)并更改输出类型组合框来更改此设置。

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.

在此处输入图像描述

于 2013-08-08T09:55:30.850 回答