Click here to Skip to main content
15,911,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i am currently developing an application using visual c# and part of the application consists of a form that has 10 pictureboxes and a combobox that allows the user to select who to load the pictures in to the pictureboxes (its either the player or the computer). now if the user selects the -player option he/she can click on to the pictureboxes to load the pictures but if the user selects the -computer option then a picture is supposed to load on to a random picturebox.
how do i make the code to load a picture on to a random picturebox?
code samples would help..

THANKS IN ADVANCE....
Posted

1 solution

You could put all the pictureboxes into an array. After that you can use Random[^] class to generate a random number between 0 and 9. Something like:
C#
Random random = new Random();
int index = random.Next(0, 9); 

Now get the picturebox from the array based on the random number and show the picture in it.
 
Share this answer
 
Comments
Sander Rossel 5-Sep-11 18:43pm    
Nice solution.
However, I'd use random.Next(0, listOfPicBoxes.Count - 1) just in case you'd ever need more picboxes (someone will forget to update that number ;) ).
My 5 though.
Wendelius 6-Sep-11 0:18am    
Thanks, yes, that would make the code more robust :)

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