2

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

    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);
4

2 回答 2

4

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

UdpClient udpc = new UdpClient(textBox1.Text, 2055); 
于 2012-10-01T08:37:13.423 回答
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;
    }
}
于 2012-10-01T08:44:01.010 回答