May be this is not the best way, you can use the following function ...
private PictureBox getPictureBoxByName(string name)
{
foreach(object p in this.Controls ){
if( p.GetType() == typeof(PictureBox) )
if( ((PictureBox)p).Name == name )
return (PictureBox)p;
}
return new PictureBox(); //OR return null;
}
The function returns PictureBox
object on your form by given the name
of string
.
And change your code like
getPictureBoxByName(RanPicture).Image =
Image.FromFile(Directory.GetCurrentDirectory() +
"\\images\\image" + imagePic + ".jpg");