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