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
Randomize Picture Box from ImageList
Page 1 of 16 Replies - 17374 Views - Last Post: 24 February 2010 - 06:56 AM
Replies To: Randomize Picture Box from ImageList
#2
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
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
#3
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
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
#4
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).
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."
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."
#5
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.
(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
#6
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.
thanks
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
#7 Guest_irfan*
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|