Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hallo, I have array with images and i want to draw only one and if i click on button, randomly draw another.
Random random = new Random();
ArrayList myImages = new ArrayList();
private void Form1_Load(object sender, EventArgs e)
        {
            myImages.Add(Properties.Resources._0);
            myImages.Add(Properties.Resources._1);
        }
private void Form1_Paint(object sender, EventArgs e)
        {
                ?
        }
private void Button1_Click(object sender, EventArgs e)
        {
                ?
        }

Thank you for answer.
Posted
Comments
CHill60 21-Feb-15 11:56am    
so what is your problem?

1 solution

I would have change
C#
ArrayList myImages = new ArrayList();

to generic List<t> which don't need casting and type safe[^]
C#
List<Image> myImages = new List<Image>();

then you can set random image from the list as below
C#
pictureBox1.Image = myImages[random.Next(0, myImages.Count)];
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-Feb-15 12:39pm    
5ed. You demonstrated a way of getting away from explaining all other aspect the inquirer is not likely aware of. ;-)
But this is fair enough: the initiative should come from the one who is in need...

You did not address the exact inquirer's questions though: what to do on paint event and on click. But this is a rare case when painting anything is not needed, and the second one is two trivial, but you could explain that you would advise the assignment you've shown in your sample line.

—SA
Member 10808387 21-Feb-15 13:24pm    
Program write an error but I figured out. Just before myImages[random.Next(0, myImages.Count)]; you must add (Image) so the code is: pictureBox1.Image = (Image)myImages[random.Next(0, myImages.Count)]. But TY for Answer :)
Zoltán Zörgő 22-Feb-15 2:05am    
If this answer helped you (most) feel free to accept it formally.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900