Kamran
Kamran

Reputation: 181

Multiple Images randomly assign to multiple picture boxes

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

Answers (1)

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34189

The algorithm is pretty straightforward:

  1. Get 20 images into an array
  2. Shuffle the array
  3. Assign images to your PictureBoxes

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

Related Questions