关于Winforms的问题

时间:2011-02-02 11:48:36

标签: c# winforms tcp

我想将我的代码转移到winform。

我知道如何创建按钮,文本等...以及如何操作它们。

问题是我在Windows应用程序中制作了我的代码,并且我想将其转换为窗体应用程序..我不知道如何复制和粘贴整个代码,导致在窗口表单应用程序中没有主要的方法..

这是我尝试转移到winfrom的代码:

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();
        }
    }
}
}

这是我创建的winform:

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)
        {

        }


    }
}

1 个答案:

答案 0 :(得分:1)

在Windows窗体应用程序中有main方法,但它在program.cs文件中,并在程序开始执行后启动默认窗体。

您可以在表单中添加文本框,并在按钮事件中获取文本框的文本。

String username = textboxusername.Text;

同样,你可以做你想做的所有事情。

之前编写的代码用于控制台应用程序,对于Windows窗体应用程序而言则不一样。

您必须为此编写不同的代码。