6 Replies - 21965 Views - Last Post: 24 February 2010 - 06:56 AM Rate Topic: -----

#1 ryand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-May 08

Randomize Picture Box from ImageList

Post icon  Posted 08 May 2008 - 11:42 AM

Hey this may be a very simplistic thing to do, but I want to be able to randomize what image the picture box displays, and I want to pick it from an Image list with the collection that I want to use.

I'm trying to use a custom function so that it can be repeated.

Thanks,
Ryan
Is This A Good Question/Topic? 0
  • +

Replies To: Randomize Picture Box from ImageList

#2 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: Randomize Picture Box from ImageList

Posted 08 May 2008 - 12:12 PM

Welcome to the dream.in.code's programming help forums! In order to better help you and other members, there are a couple rules we'd like you to follow:
Post the code you have so far using the [code] tags. We can't help you if you don't post your code. We will not do your homework for you. You MUST show some effort.
We have a 0-tolerance cheating policy. If we find you are trying to cheat, you will be warned, and subsequently banned. You MUST show some effort.
Make your questions and answers clear and concise. Explain the problem, any errors you are receiving, and the code causing the errors.
Utilize the "Search" feature. Many questions have already been asked and answered in another thread.
If you are answering someoneā€™s question, only do so if they have made an attempt. We will delete any posts providing code when a user has not shown some effort. Kindly ask the user to post what they've done so far.
If you absolutely must hire someone to do your code, do NOT post in the forums. All correspondence must be over PM or E-Mail.
Following these simple guidelines helps us help you faster and provides others in the future a way to learn. Thank you for your cooperation.

In other words, You do some work, we will help you from there
Was This Post Helpful? 0
  • +
  • -

#3 ryand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-May 08

Re: Randomize Picture Box from ImageList

Posted 08 May 2008 - 02:14 PM

here is what code I have, I tried using an array, but I'm not sure if thats the best way:

Private RandomImage() as Image {ImageList1.Images.Item(1), ImageList1.Images.Item(2), etc...}

...

Private Sub btnRandomize_Click...

Dim rnd as new random
Pic1 = rnd.RandomImage (This is my problem spot)

End Sub

This post has been edited by ryand: 08 May 2008 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Randomize Picture Box from ImageList

Posted 08 May 2008 - 03:01 PM

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).

    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." :snap:
Was This Post Helpful? 1

#5 ryand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-May 08

Re: Randomize Picture Box from ImageList

Posted 08 May 2008 - 06:48 PM

thank you very much, I'll try it out.

(5 minutes later)
Thank you very much, it works perfectly, appreciate the assistance.

This post has been edited by ryand: 08 May 2008 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 ballin9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-December 08

Re: Randomize Picture Box from ImageList

Posted 03 December 2008 - 02:40 PM

sorry to bump an old thread... the code above works perfectly but how can i do it so the same image doesnt get repeated more then twice. so when two of the same images are already picked, it goes on to select a different image until there are pairs of everything.

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic1.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic2.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic3.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic4.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic5.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic6.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic7.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic8.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic9.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic10.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic11.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic12.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic13.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic14.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic15.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
		pic16.Image = imglistPics.Images(intPic)
		intPic = rnd.Next(0, imglistPics.Images.Count)
	End Sub


thanks :)
Was This Post Helpful? 0
  • +
  • -

#7 Guest_irfan*


Reputation:

Re: Randomize Picture Box from ImageList

Posted 24 February 2010 - 06:56 AM

i want to add images into imagelist from picturebox at runtime, what will be the code for this. i have tried this
dim img as system.drawing.image.fromfile(picturebox1.image)
imagelist1.images.add(img)
but it cant help me, what i can do now. plz help me.
Was This Post Helpful? 0

Page 1 of 1