mardi 3 octobre 2023

C# using a picture box to get a randomized picture

I have imported 5 images into the resources folder on my project. The image names are Blackcar, Redcar, Bluecar, Yellowcar, Greencar. When I run this program I want the picturebox to randomly display one of these images so it is different everytime I run this program.

    private void Form1_Load(object sender, EventArgs e)
    {
        String[] imgs = { "Blackcar", "Bluecar", "Greencar", "Yellowcar," "Redcar" };
        int randomIndex;
        Random r = new Random();

        randomIndex = r.Next(imgs.Length);
        pictureBox1.Image = imgs[randomIndex];
    }

I know i cannot convert the string array to the picture box, but Im not sure how to make this work.




Aucun commentaire:

Enregistrer un commentaire