views:

86

answers:

2

Hi everybody. I'm new to programming in C#. My question is this;

how can i, after generating random numbers (i got so far), get these numbers to show random images in imageboxes?

I have 4 imageboxes with a backgroundimage. (but i want them to change, according to the numbers i generate).

How do i do that?

Anybody got an idea?

Thx in advance!!

+7  A: 

Keep a list of images, and set the image to a random element in the list:

public class SomeHostingForm
{
    private readonly List<Image> images; // Populate elsewhere...
    private readonly Random random = new Random();

    private void SwitchImage(ImageBox box)
    {
        box.Image = images[random.Next(images.Count)];
    }
}

(I'm not sure exactly which type you're talking about, so this is somewhat pseudo-code.)

Jon Skeet
+3  A: 

Put the imageboxes in an array, then use the random number as the index of the array to get a random imagebox.

Konamiman