如何将值附加到另一个类的表单的文本框中

时间:2016-03-10 10:40:36

标签: c# asp.net winforms

您好我是c#的初学者。我需要从另一个类(udp)向Form(udpsendrecv)中的文本框追加一个值。我搜索了很多答案,但没有一个与我的问题类似。

这是我的表单和类代码。

我在字符串输出中得到的值将类似于" 2d346e234d56"。我需要将输出的子字符串附加到udpsendrecv形式的文本框中。我怎样才能做到这一点?我通过引用不同的例子尽我所能。你的答案对我很有帮助。谢谢

public partial class udpsendrecv : Form
{
    public udpsendrecv()
    {
        InitializeComponent();
    }

    public void AppendTextBox(string value)
    {
        if (InvokeRequired)
        {
            this.BeginInvoke(new Action<string>(AppendTextBox), new object[] { value });
            return;
        }
        textBox1.Text = value;
    }
      public void AppendTextBox1(string value)
    {

        if (InvokeRequired)
        {
            this.BeginInvoke(new Action<string>(AppendTextBox1), new object[] { value });
            return;
        }
        textBox2.Text = value;
    }

    public void AppendTextBox2(string value)
    {

        if (InvokeRequired)
        {
            this.BeginInvoke(new Action<string>(AppendTextBox2), new object[] { value });
            return;
        }
        textBox3.Text = value;
    }
    public void Textboxclear()
    {
        if (InvokeRequired)
        {
            this.BeginInvoke(new Action<string>(AppendTextBox1), new object[] { });
            return;
        }

        textBox1.Clear();
    }

    public void UpdateTextBox(string text)
    {
        Invoke((MethodInvoker)delegate {
            textBox1.AppendText(text + "\r\n");
        });
    }
 }}

class UDP
{
public  void Receive(IAsyncResult ar)
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER);
        byte[] bytes = udp.EndReceive(ar, ref ip);
        string output= BitConverter.ToString(bytes);
        int chunkSize = 4;
        int stringLength = output.Length;
        string[] Elements = new string[25];
        int j = 0;
        for (int i = 0; i < stringLength; i += chunkSize)
        {
            if (i + chunkSize > stringLength)
                chunkSize = stringLength - i;

            Elements[j] = output.Substring(i, chunkSize);
            j++;
        }
         udpsendrecv hi = new udpsendrecv();

        short a = Convert.ToInt16(Elements[0], 16);
        hi.AppendTextBox(a.ToString());
        short b = Convert.ToInt16(Elements[1], 16);
        hi.AppendTextBox1(b.ToString());
        short c = Convert.ToInt16(Elements[2], 16);
        hi.AppendTextBox2(c.ToString());

 }}

0 个答案:

没有答案
相关问题