简体   繁体   中英

Display many Pictures in C#

I have written code to display many image (100 image) with PictureBox but when run the application, just one image has been shown...

please help me guys...

Here is my code:

    Random r = new Random();

    private  int randomPoint()
    {

        return 1 + r.Next() % 15;

    }

    // x0, y0
    private int[] initialLocation = new int[2];

    private void setLocation(int i)
    {
        int x0 = 50, y0=50;

        initialLocation[1] = y0;
        switch (i)
        {
            case 1: initialLocation[0] = x0;
                break;

            case 2: initialLocation[0] = x0 + 50;
                break;

            case 3: initialLocation[0] = x0 + 100;
                break;

            case 4: initialLocation[0] = x0 + 150;
                break;

            case 5: initialLocation[0] = x0 + 200;
                break;

            case 6: initialLocation[0] = x0 + 250;
                break;

            case 7: initialLocation[0] = x0 + 300;
                break;

            case 8: initialLocation[0] = x0 + 350;
                break;

            case 9: initialLocation[0] = x0 + 400;
                break;

            case 10: initialLocation[0] = x0 + 450;
                break;

        }

    }

    public Form1()
    {
        InitializeComponent();

        PictureBox[] p = new PictureBox[10];


        for (int i=0; i<10;i++)  
        {

            p[i] = new PictureBox();
            p[i].ImageLocation = "1.png";

            int[] l = new int[2];

            // create random location for images
            setLocation(randomPoint());

            p[i].Location = new Point(initialLocation[0], initialLocation[1]);

            this.Controls.Add(p[i]);
        }
   }

It's because you are declaring your random number generator every time you want an image:

private  int randomPoint()
{
    Random r = new Random();

    return 1 + r.Next() % 15;
}

replace this with:

private Random r = new Random();

private  int randomPoint()
{
    return 1 + r.Next() % 15;
}

UPDATE

If I understand you correctly you want to display 15 images in a random order across the form.

To ensure that you don't get any repeats you need to make sure that you remove the picture you've picked from the list before picking the next. So (in pseudo code) you need something like this:

this.folderList = new List<string>();
// Populate it in an orderly manner

// Create temporary list to put the randomly selected items into
var radomisedList = new List<string>();

// Randomise the list.
var random = new Random();
int num = 0;

while (this.folderList.Count > 0)
{
    num = random.Next(0, this.folderList.Count);
    radomisedList.Add(this.folderList[num]);
    this.folderList.RemoveAt(num);
}

This will ensure you get a random order and no repeats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM