|
-
Aug 20th, 2007, 07:04 AM
#1
[RESOLVED] [02/03] Random pictures
Hi All,
I'm trying to do a random with pictures in a Imagelist.
Here's the code I'm using:
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'Rnd() = 0,9672324
'Rnd() * 10 = 9,672324
'CInt(rnd() * 10) = 10
Dim myNr As Integer
myNr = CInt(Rnd() * Me.ImageList1.Images.Count)
Me.PictureBox10.Image = Me.ImageList1.Images(myNr)
End Sub
It's working but after some clicks it gaves me an error:
Additional information: Specified argument was out of the range of valid values.
What's wrong with this code.
Thanks in advance,
sparrow1
-
Aug 20th, 2007, 07:32 AM
#2
Re: [02/03] Random pictures
Its obviously returning values outside of the index bounds.
I'd recommend you to use the Random class rather than the Rnd() function.
VB Code:
Private rand As New Random
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
myNr = Rand.Next(0, Me.ImageList1.Images.Count)
Me.PictureBox10.Image = Me.ImageList1.Images(myNr)
End Sub
Last edited by Atheist; Aug 20th, 2007 at 07:36 AM.
-
Aug 20th, 2007, 07:34 AM
#3
Re: [02/03] Random pictures
Delare the random object as a class variable and in your button.click event handler, you call random.next(0, Me.ImageList1.Images.Count)
Code:
Private rand As New Random
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim count as Integer = Me.ImageList1.Images.Count
If count > 0 Then
Dim myNr As Integer = Me.rand.Next(0, count)
Me.PictureBox10.Image = Me.ImageList1.Images(myNr)
End If
End Sub
-
Aug 20th, 2007, 07:55 AM
#4
Re: [02/03] Random pictures
Thanks, there both working.
Wkr,
sparrow1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|