简体   繁体   English

C#控制台应用程序作为Windows窗体

[英]C# Console App as a Windows Form

I started a project as a console app in visual studios and I'm now finding that I need a UI (form) but when I run the program it beings up a console. 我在视觉工作室中以控制台应用程序开始了一个项目,现在发现我需要一个UI(窗体),但是当我运行该程序时,它就是一个控制台。 Is there anyway of having my Program.cs code from my console app run from a button event? 无论如何,是否需要通过按钮事件运行控制台应用程序中的Program.cs代码?

Spinet of my Code: 我的代码的尖刺:

    using System;
using System.Net.Sockets;
using System.IO;
public class Program
{

    public static string name = null;
    public static string userloc = null;
    public static string protocol = null;

    public static int Portal = 43;
    public static string Address = "whois.net.dcs.hull.ac.uk";

    static void Main(string[] args)
    {


        for (int i = 0; i < args.Length; i++)
        {
            switch (args[i])
            {
                case "-h0":
                    protocol = "-h0";
                    break;

                case "-h1":
                    protocol = "-h1";
                    break;

                case "-p":
                    Portal = int.Parse(args[i + 1]);
                    ++i;
                    break;

Form Code: 表格代码:

namespace location
{
    public partial class Location : Form
    {
        public Location()
        {
            InitializeComponent();
        }

        private void updateBtn_Click(object sender, EventArgs e)
        {

        }
    }
}

Most of what you require is at How do I convert a .NET console application to a Winforms or WPF application . 您最需要的是如何将.NET控制台应用程序转换为Winforms或WPF应用程序

You have then asked in comments "how I can have my button on my form run the Main() code?". 然后,您在注释中询问“我如何让表单上的按钮运行Main()代码?”。 I'm assuming you know that the updateBtn_Click method will get executed when updateBtn is clicked, and I'm assuming that's the button you're referring to as "my button on my form". 我假设您知道单击updateBtn时将执行updateBtn_Click方法,并且我假设这是您称为“我的表单上的按钮”的按钮。

The answer is that you wouldn't want to have the button click call Program.Main , since that is the entry point for the application. 答案是,您不想让按钮单击调用Program.Main ,因为那是应用程序的入口点。

I'm also assuming that you want to keep the command line parameters, so you need to leave the processing of args in Program.Main . 我还假设您想保留命令行参数,因此需要将args的处理留在Program.Main

Whatever the rest of your functionality is, you could move it directly into your form, into the updateBtn_Click method. 无论其余功能是什么,您都可以将其直接移到表单中,并updateBtn_Click方法中。 The code may need to be tweaked to get the values of the command line parameters from there by prefixing their names with Program. 可能需要对代码进行调整,以在命令行名称前加上Program.来获取命令行参数的值Program. since they are all static (eg Program.Portal ). 因为它们都是静态的(例如Program.Portal )。

This will probably satisfy your requirements for now. 目前,这可能会满足您的要求。 Over time, you will realise that cramming code into "code behind" (which is what I've just told you to do) isn't a great idea. 随着时间的流逝,您将意识到将代码塞入“后面的代码”(这是我刚刚告诉您要做的)并不是一个好主意。 If you Refactor your code, then you would be able to use your functionality from the command line, or the WinForm app, or maybe a WPF or Web app. 如果您重构代码,则可以从命令行,WinForm应用程序或WPF或Web应用程序使用功能。 But maybe it's good enough for now... 但是也许现在已经足够了...

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

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