jeudi 10 octobre 2019

How to get in same array different ids for random generation

I'm trying to make the picture boxes show random images from the folder, but they are always showing the same image, i added a +1 in the index but the problem is they always show the same combination.

private string[] files;
private int currentIndex = 0;    

private void initializeImages()
{
    string appRoot = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
    files = System.IO.Directory.GetFiles(appRoot + @"\Resources");
    Random rnd = new Random();
    files = files.OrderBy(x => rnd.Next()).ToArray();
}
private void setImage()
{
    pictureBox1.ImageLocation = files[currentIndex];
    pictureBox2.ImageLocation = files[currentIndex+1];
}

private void nextImage()
{
    currentIndex = currentIndex < files.Length - 1 ? currentIndex + 1 : files.Length - 1;
    setImage();
}

private void button1_Click(object sender, EventArgs e)
{
    initializeImages();
    nextImage();
}



Aucun commentaire:

Enregistrer un commentaire