Lynn Emery

Lynn Emery

  • NA
  • 4
  • 18.9k

Label text & listBoxItems not updating on a thread C#.

May 12 2016 11:38 AM
Hi,
I have several labels and listboxlists on my windows form i am trying to update these with new data whilst connected to a serial com port.
At first i got an error stating that these items could not be accessed as they were developed on another thread, however i got around this by invoking and using delegates.
 
delegate void MyDelegate(string text);
private void UpdateListBox(string text)
{
listBox1.Items.Add(text);
}
delegate void TBDelegate(string text);
private void UpdateTextField(string text)
{
label5.Text = text;
}
listBoxProcessing.Invoke((MethodInvoker)(() => listBoxProcessing.Items.Add(textBoxEntry.Text.ToString().Trim())));
 
 
Invoke(new MethodInvoker(delegate(){listBoxProcessing.Items.Add(textBoxEntry.Text.ToString().Trim()}
 
if (listBoxProcessing.InvokeRequired)
//{
// this.Invoke(new MethodInvoker(delegate()
// {
// listBoxProcessing.Items.Add(textBoxEntry.Text.ToString().Trim());
// //Access your controls
// }));
 
Invoke(new TBDelegate(UpdateTextField),"Data for the Textbox"); 
 
 After doing the above I now have no error but the labels and listboxitems are still not updating but i can see that they are being assigned the correct data it is just not being displayed on the form.
 
I can see that labels have kind of been updated but the data is hidden behind the default label names, it is as if there is 2 layers of the form which are being presented. So I can see the word label 5 and the data which label 5 should be displayed. However this is only the case for the labels and not the listbox items, the list box items do not present any information.
 
Has anyone experienced this before or does anyone know how to over come this problem?
Many Thanks 
 

Answers (1)