I am a real newcomer to c#, and do not have previous programming skills, but need some help, please.
I have a winform with pictureboxes, and I want to randomly display images in these pictureboxes from an array of images. Is this possible, and, if so, how is it done?
Many Thanks
AlanPosted Sep 17, 2008, 11:57 AM
First place this static field in your form class:
static Random rand = new Random();
Suppose now that 'images' is your Image array and that 'pictureBoxes' is your PictureBox array. I'm assuimg that the number of pictureboxes is less than or equal to the number of images. The following code will place a (different) random image in each PictureBox:
bool[] isUsed = new bool[images.Length]; // all false by default
int index = 0;
do
{
int imageNum = rand.Next(0, images.Length); // includes 0 but excludes images.Length
if (!isUsed[imageNum])
{
pictureBoxes[index].Image = images[imageNum];
isUsed[imageNum] = true;
index++;
}
}
while (index < pictureBoxes.Length);