The following piece of code gets all labels named "label_bluep"+i where i is between 1 and 5.
for (int i = 1; i < 5; i++)
{
Label labelname = (Label)this.Controls.Find("label_bluep" + (i), false).FirstOrDefault();
labelname.Parent = pictureBox_background;
}
When the code runs, this works fine. If I called the Controls.Find() method again for the same object e.g copying and pasting that same code to get this:
for (int i = 1; i < 5; i++)
{
Label labelname = (Label)this.Controls.Find("label_bluep" + (i), false).FirstOrDefault();
labelname.Parent = pictureBox_background;
}
for (int i = 1; i < 5; i++)
{
Label labelname = (Label)this.Controls.Find("label_bluep" + (i), false).FirstOrDefault();
labelname.Visible = true;
}
I get the exception labelname was null.
What's the reason for this?