Results 1 to 4 of 4

Thread: [RESOLVED] [02/03] Random pictures

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Resolved [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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Private rand As New Random
    2.  
    3. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    4.         myNr = Rand.Next(0, Me.ImageList1.Images.Count)
    5.         Me.PictureBox10.Image = Me.ImageList1.Images(myNr)
    6.     End Sub
    Last edited by Atheist; Aug 20th, 2007 at 07:36 AM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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

  4. #4

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [02/03] Random pictures

    Thanks, there both working.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width