简体   繁体   English

如何将代码从控制台应用程序传输到 WinForms?

[英]How to transfer code from Console application to WinForms?

I want to transfer my code to a WinForms.我想将我的代码转移到 WinForms。

I know how to create buttons, text, etc... and how to operate them.我知道如何创建按钮、文本等......以及如何操作它们。

The problem is that I made my code in a windows application, and I want to transform it to WinForms application.问题是我在 Windows 应用程序中编写了代码,我想将其转换为 WinForms 应用程序。 I don't know how to copy and paste the whole code, cause in the WinForms application there is no main method..我不知道如何复制和粘贴整个代码,因为在 WinForms 应用程序中没有 main 方法..

Here is my code that I am trying to transfer to WinForms:这是我试图转移到 WinForms 的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient connection = new TcpClient("127.0.0.1", 5000);
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
       
            string name2 = "";
            while (true)
            {
                Console.WriteLine("Enter your name and press submit");
                name2=Console.ReadLine();
                if (name2 != "")
                {
                    sw.WriteLine(name2);
                    break;
                }
            }
            Console.WriteLine("Loop is over");
            Thread t2 = new Thread(Reader);
           
            t2.IsBackground = true;
            t2.Start(connection);

            while (true)
            {
                sw.WriteLine(Console.ReadLine());
                sw.Flush();
            }
        }




    public static void Reader(object o)
    {
        TcpClient con = o as TcpClient;
        if (con == null)
            return;
        StreamReader sr = new StreamReader(con.GetStream());
        while (true)
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine();
            }

            Console.WriteLine(sr.ReadLine());
            Console.WriteLine();
        }
    }
}
}

Here is the Form that I have created:这是我创建的表单:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

      
    }
}

In windows form application there is main method but it is in program.cs file and its starts your default form after you program starts execution.在 windows 窗体应用程序中有 main 方法,但它位于 program.cs 文件中,并且在程序开始执行后它会启动您的默认窗体。

You can add the the textbox in form and on button event get the text of the textbox.您可以在表单中添加文本框,并在按钮事件中获取文本框的文本。

String username = textboxusername.Text;

Similarly you can do all the things you want.同样,您可以做所有您想做的事情。

The code that you have written before was for console application and that is not the same for the windows form application.您之前编写的代码是针对控制台应用程序的,而对于 Windows 窗体应用程序则不同。

You have to code differently for this.您必须为此编写不同的代码。

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

相关问题 如何从控制台应用程序正确关闭winforms应用程序 - How to properly close winforms application from console application 如何在Winforms应用程序中嵌入控制台应用程序 - How to embed a Console application inside a Winforms application 如何在.Net Framework 3.5的WinForms中从控制台应用程序接收输出? - How should I receive output from Console application in WinForms on .Net Framework 3.5? 如何从控制台应用程序到表单应用程序使用代码 - How to use a code from a console application to a form application 在 winforms 应用程序中从 Progarm.cs 运行控制台 - Running a console from Progarm.cs in a winforms application 从控制台应用程序发送输入/获取输出(C#/ WinForms) - Sending input/getting output from a console application (C#/WinForms) 如何在WinForms C#中将数据从一种形式传输到另一种形式? - How to transfer data from one form to another in winforms c#? winforms GUI 应用程序中的控制台输出 - Console output in winforms GUI application DLL在WinForms中工作,但不在控制台应用程序中工作 - DLL working in WinForms but not in console application Mono上的WinForms / Console应用程序,如何知道它以root身份运行 - WinForms/Console application on Mono, how to know it runs as root
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM