0

Hello i am begginer in c#. I Need to append a value to a textbox in a Form(udpsendrecv) from a another class(udp). I searched for many answers but none of them are similar to my question.

Here are my form and class code.

The value i get in string Output will be like "2d346e234d56". I Need to append the substring of Output to a textboxs in the form udpsendrecv. how can i do this? i tried my best by refering different examples. Your answers will help me lot. Thank you

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

 }}
  • Build your text in a static method in that class and call it from your "Form" class and set the textbox text. – Zaki Mar 10 '16 at 10:44
  • There are plenty of answers on this. How to pass a value [using the constructor](http://stackoverflow.com/questions/4247807/passing-variable-between-winforms) and how to pass a value [using properties](http://stackoverflow.com/questions/6259030/how-to-pass-string-value-from-one-form-to-another-forms-load-event-in-c). – Equalsk Mar 10 '16 at 10:47
  • Thank you very much, the already answered question helped me. – hariprasad Gowda Mar 14 '16 at 12:29

0 Answers0