Reputation: 181
I am working on windows form where I have 20 Picturebox
in my winform and 20 images in project folder.
My question is How Can I assign images randomly to Picture boxes.
For Example: On button click - images randomly assign to Pictureboxs
Upvotes: 0
Views: 126
Reputation: 34189
The algorithm is pretty straightforward:
Assuming that you generate and store your picture boxes in an array, it would look like:
string[] shuffledImages = Directory.GetFiles(".", "*.png")
.OrderBy(x => Guid.NewGuid())
.ToArray();
for (int i = 0; i < 20; i++)
pictureBoxes[i].Image = Image.FromFile(shuffledImages[i]);
Any other changes or improvements are up to you :)
Upvotes: 3