Results 1 to 12 of 12

Thread: Random pictures loaded in picturebox from my.resources

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    44

    Random pictures loaded in picturebox from my.resources

    Hi guys,
    I have one little dillema:

    I have a picture box and 10 images I want to load randomly. I cannot use imagelist because these images have a width of 400 and imagelist limits the width to 256. I have added these images to my.resources.
    The thing is I cannot figure out how can I randomly load each of these pictures to this picturebox
    Any help will be greatly appreciated.
    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Random pictures loaded in picturebox from my.resources

    try this:

    vb Code:
    1. dim images() as bitmap = {my.resources.imagename1,my.resources.imagename2...my.resources.imagename10}
    2.  
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.     picturebox1.image = images(new random().next(0,10))
    5. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    44

    Re: Random pictures loaded in picturebox from my.resources

    Thanks. I was using something similar inside a function and I had some errors. Your solution works. Thanks

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    44

    Re: Random pictures loaded in picturebox from my.resources

    One more thing though, If I want to add a certain link to each picture, how do I know when a certain picture is loaded into the picturebox?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Random pictures loaded in picturebox from my.resources

    use the picturebox tag property to store the image array index

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    44

    Re: Random pictures loaded in picturebox from my.resources

    how can I do this in code?
    Thanks

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Random pictures loaded in picturebox from my.resources

    what sort of link do you want to associate with each image? will it always be the same?

    heres how to assign the array index to the picturebox. let me know about the link. we might be able to improve it.

    vb Code:
    1. dim images() as bitmap = {my.resources.imagename1,my.resources.imagename2...my.resources.imagename10}
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
    3.     dim index as integer = new random().next(0,10)
    4.     picturebox1.image = images(index)
    5.     picturebox1.tag = index
    6. End Sub

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    44

    Re: Random pictures loaded in picturebox from my.resources

    different hyperlink for each image

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Random pictures loaded in picturebox from my.resources

    vb Code:
    1. dim images() as bitmap = {my.resources.imagename1,my.resources.imagename2...my.resources.imagename10}
    2. dim links() as string = {"link1","link2",...,"link10"}
    3.  
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        
    5.     dim index as integer = new random().next(0,10)
    6.     picturebox1.image = images(index)
    7.     picturebox1.tag = links(index)
    8. End Sub
    9.  
    10. Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    11.     MsgBox(PictureBox1.Tag)
    12. End Sub

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    44

    Re: Random pictures loaded in picturebox from my.resources

    I want to make the links available on PictureBox1_Click event. Each time I click a image displayed in the picturebox should take me to the corresponding hyperlink for that image.
    Any ideas on that?

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Random pictures loaded in picturebox from my.resources

    vb Code:
    1. Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    2.     if PictureBox1.Tag isnot nothing then process.start(PictureBox1.Tag)
    3. End Sub

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    44

    Re: Random pictures loaded in picturebox from my.resources

    It works great. Thanks

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