[英]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.