Is there an easier way (less lines of code) to assign random pictures to multiple pictureBoxes?

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
The code im using right now is:


string folder = @"G:Stock Stuff For CreativePics1";
string[] piclist = Directory.GetFiles(folder, "*.jpg");

int numpics = piclist.GetLength(0);

pictureBox1.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox2.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox3.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox4.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox5.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox6.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox7.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox8.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox9.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox10.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox11.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox12.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox13.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox14.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox15.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox16.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox17.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);
pictureBox18.ImageLocation = string.Format("{0}", piclist[rnd.Next(0,numpics)]);


Thank you for your help!

View the full article
 
Back
Top