I want to use a string value like this
Random rnd = new Random();
int x= rnd.Next(1, 10);
string ime = "pictureBox" + x.ToString();
ime.BackColor = Color.CornflowerBlue;
but that doesnt work
I want to use a string value like this
Random rnd = new Random();
int x= rnd.Next(1, 10);
string ime = "pictureBox" + x.ToString();
ime.BackColor = Color.CornflowerBlue;
but that doesnt work
You don't really want to use a string like this. You want to get a control with that name and use it like this. You can get a control by name like follows:
var pictureBox = myForm.Controls[ime];
Following code should work for you,
Random rnd = new Random();
int x= rnd.Next(1, 10);
string ime = "pictureBox" + x.ToString();
((PictureBox)frm.Controls.Find(ime ,true)[0]).BackColor = Color.CornflowerBlue;
Instead of generating a random name and using it do find the PictureBox with that name, directly pick a random PictureBox:
Random rnd = new Random();
var pictureBoxes = frm.Controls.OfType<PictureBox>().ToArray();
var randomPictureBox = pictureBoxes[rnd.Next(pictureBoxes.Length)];
randomPictureBox.BackColor = Color.CornflowerBlue;