0

Whit this code i'm trying to shuffle som pictures, but i get only one picture on screen.

Can someone tell me what i'm missing? The ilist has 9 different values, but it is only the first values that gets on screen. All the pictures are in the folder.

Thanks in advance.

protected void btnShuffle_Click(object sender, EventArgs e)
{
    //int[] values = {1,2,3,4,5,6,7,8,9 };
    List<int> MyRandomList = Shuffeld();

    List<int> ilist = new List<int>();

    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[0] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[1] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[2] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[3] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[4] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[5] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[6] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[7] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[8] + ".jpg");


}
public static List<int> Shuffeld() {

    List<int> ilist = new List<int>();

    while (ilist.Count < 9)
    {
        Random r = new Random();
        int a = r.Next(1, 10);
        while (!ilist.Contains(a))
        {
            ilist.Add(a);
        }
        }


    return ilist;

}
TheNewone
  • 97
  • 1
  • 10

2 Answers2

1

I think you always set your Attributes["src"] to the same image object: img1.

If it's not the case you should use a for... Anyway, you should use a for.

Edit: An example of for loop.

for (int i = 0; i < 9; i++)
    {
        imgList[i].Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[i] + ".jpg")
    }

(I supposed you have a list of object image.)

0

You need one image control for each image you wish to display

The problem is you only have one image on the page, img1. Obviously you can't display nine images with only one image on the page. You need nine.

The most obvious (not necessarily most elegant) fix is to change...

    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[0] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[1] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[2] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[3] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[4] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[5] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[6] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[7] + ".jpg");
    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[8] + ".jpg");

...to...

    img1.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[0] + ".jpg");
    img2.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[1] + ".jpg");
    img3.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[2] + ".jpg");
    img4.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[3] + ".jpg");
    img5.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[4] + ".jpg");
    img6.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[5] + ".jpg");
    img7.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[6] + ".jpg");
    img8.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[7] + ".jpg");
    img9.Attributes["src"] = ResolveUrl("~/Stendalen3/gfx/Puslespil/" + MyRandomList[8] + ".jpg");

And of course make sure img1, img2, img3, img4, img5, img6, img7, img8, and img9 are all defined on the page.

Now if you want an elegant solution, maybe you can find a way to use an array of image controls instead of nine separate controls, or perhaps embed your image controls in a repeater. Then you could use a for loop instead of writing one line of code for each image. But that would be a topic for a different question.

P.S.

If you need to generate a list of numbers from 1 to 9 in random order, a real quick shortcut is this:

Random r = new Random();
var randomList = Enumerable.Range(1, 9).OrderBy( a => r.Next());
John Wu
  • 50,556
  • 8
  • 44
  • 80