i want to execute a piece of code when a labels backcolor is changed..
following is the code
private void lbl_ps_BackColorChanged(object sender, EventArgs e)
{
if (lbl_ps.BackColor == Color.Green)
{
menu_ctrl.setup_array = str_array;
indx = 0;
}
}
here 'menu_ctrl' is a usercontrol where this setup_array consists of following code
public string[] setup_array
{
get
{
return list_array;
}
set
{
list_array = value;
setup_list(value);
}
}
private void setup_list(string[] list_item)
{
//this.Width = 266;
this.Height = 181;
for (int i = 0; i < n; i++)
{
this.Controls.Remove(labels[i]);
}
label1.Visible = false;
this.BorderStyle = BorderStyle.None;
indx_abs = 0; indx_rel = 0;
n = list_item.Length;
Array.Resize(ref labels, n);
for (int i = 0; i < n; i++)
{
labels[i] = new CustomText();
labels[i].Left = 20;
labels[i].Top = ((30) * i)+1;
labels[i].IsSelected = false;
this.Controls.Add(labels[i]);
labels[i].Text = list_item[i];
}
labels[indx_abs].IsSelected = true;
}
but the code wont execute....it works fine if i call this code from a button click event.. but it wont execute in backcolorchange event
any help?
EDIT:i tried break point and i got this error in the first for loop in setup_list function
Cross-thread operation not valid: Control 'CustomText' accessed from a thread other than the thread it was created on.
the error was caught deep inside a reference dll i've been using. btw a function in this dll is raising this colorchanged event
thanks to @Nati Dobkin!!
i was able to identify the the code trigger was being created on a new thread.. and found this on the internet to force run a code in main thread.
this.Invoke(new MethodInvoker(() =>
{
//run your code here
}));