I'm trying to change the image of a pictureBox
based on the number of occurance of a list:
list<string> items = new List<string>();
items.Add("Item1");
items.Add("Item2");
items.Add("Item3");
items.Add("Item4");
items.Add("Item5");
foreach (var item in items.OfType<string>().Select((x, i) => new { x, i }))
{
int ItemNumber = item.i + 1;
string ItemNumberStr = ItemNumber.ToString();
PictureBox pbox = (PictureBox)this.Controls["Picturebox" + ItemNumberStr];
pbox.Image = Properties.Resources.white_square_button;
Label labl = (Label)this.Controls["label" + ItemNumberStr];
labl.Text = item.x;
}
This is done within a foreach
event in which item.i
is the number of occurance represented as an int
, then converted to a string
to determine the number of pictureBox
I'm trying to modify. But when I do this I recieve error "Object reference not set to an instance of an object" here:
pbox.Image = Properties.Resources.white_square_button;
This also happens with the label
aswell.
What am I doing wrong?