Check out the function below. This function will set our picturebox (titled PictureBox1) to a random image contained in our imagelist control (titled ImageList1) whenever the user clicks the button (titled Button1).
vb
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Create a integer and new Random object
Dim intPic As Integer
Dim rand As New Random
' Pick a random number between 0 and the number of images in our imagelist control
intPic = rand.Next(0, ImageList1.Images.Count)
' Now set the picturebox image equal to the image chosen from the imagelist box randomly using the random
' integer
PictureBox1.Image = ImageList1.Images(intPic)
End Sub
What we do is pick a random number from 0 to the number of images in the imagelist. We then use this random number to then access the image at that index from the imagelist and set that to the picturebox.
Enjoy!
"At DIC we be random picture picking code ninjas... we are less picky on which we pick, taco del mar, taco time, or taco bell. Ok just kidding, we are picky on that too."