将此代码转换为按钮单击事件

时间:2012-10-01 08:31:56

标签: c# winforms

我想将此控制台代码转换为表单代码:

    public void catching(string[] args)
    {
        UdpClient udpc = new UdpClient(args[0], 2055);
        IPEndPoint ep = null;
        while (true)
        {
            Console.Write("Name: ");
            string name = Console.ReadLine();
            if (name == "") break;
            byte[] sdata = Encoding.ASCII.GetBytes(name);
            udpc.Send(sdata, sdata.Length);
            byte[] rdata = udpc.Receive(ref ep);
            string job = Encoding.ASCII.GetString(rdata);
            Console.WriteLine(job);
        }
    }                                                                                        

我想把它放到按钮click事件中:

private void button1_Click(object sender, EventArgs e)
{

}

但是我在这行中出错了:

UdpClient udpc = new UdpClient(args[0], 2055);

2 个答案:

答案 0 :(得分:4)

没有像控制台应用程序那样的args变量。您必须为用户输入放置TextBox并使用该值。即。

UdpClient udpc = new UdpClient(textBox1.Text, 2055); 

答案 1 :(得分:0)

private void button1_Click(object sender, EventArgs e)
{
     //Send the input message using a form input like RichTextBox control.
     string text = this.richTextBox1.Text;
     UdpClient udpc = new UdpClient(text, 2055);
     IPEndPoint ep = null;
     while (true)
     {
        string name = this.richTextBox2.Text;
        if (name == "") break;
        byte[] sdata = Encoding.ASCII.GetBytes(name);
        udpc.Send(sdata, sdata.Length);
        byte[] rdata = udpc.Receive(ref ep);
        string job = Encoding.ASCII.GetString(rdata);
        this.label1.Text = job;
    }
}