简体   繁体   English

是否可以通过控制台应用程序运行Windows窗体应用程序?

[英]is it possible to run a windows form application with the console application?

Is it possible to run a windows form application with the console application? 是否可以通过控制台应用程序运行Windows窗体应用程序?

And if so, how would i use the Main() void? 如果是这样,我将如何使用Main()void?

And how could i pass strings over from the windows form to the console? 我如何将字符串从Windows表单传递到控制台?

I think that what you are saying is that you want to have a console application that can simply open a form. 我认为您的意思是希望拥有一个可以简单地打开表单的控制台应用程序。 You can do this. 你可以这样做。

Create your console application as per normal but then add your form to your project (or a reference to a project that contains the form). 按照常规创建控制台应用程序,然后将表单添加到项目(或对包含表单的项目的引用)中。 Then you just want something like this. 然后,您只想要这样的东西。

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("I'm going to open a form now!");
            var form = new Form1();
            form.ShowDialog();
        }
    }
}

If you want to add information to your form, you could add properties to the form or a add a custom constructor which takes string information you want sent to the form. 如果要向表单添加信息,则可以向表单添加属性,或者添加自定义构造函数,该构造函数接受要发送到表单的字符串信息。

First Create the windows Application like normally you do. 首先像通常一样创建Windows应用程序。

Create a new Console Application 创建一个新的控制台应用程序

In solution Explorer, goto References->Add Reference->Click Browse Tab->Select Project Path to Debug->Select the Application 在解决方案资源管理器中,转到“引用”->“添加引用”->单击“浏览”选项卡->“选择要调试的项目路径”->“选择应用程序”

Also add a Reference to System.Windows.Form; 还添加对System.Windows.Form的引用;

In the code 在代码中

Include the namespace of the Application using NewForm;// Example 使用NewForm包含应用程序的名称空间; //示例

 class Program
{
    static void Main(string[] args)
    {
        TestForm t = new TestForm(); //Class of WindowsFormApplication NewForm 
        t.ShowDialog();

    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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