1.11M Members

load images from folder to picturebox random number

 
0
 

Hi everyone. I'm new in Visual Basic(i work in Visual basic 2010) and i have a problem and hope u will help me. I want to load image to pictureBox depending on randomly generated number. For example if random number is 14 then i want to load image with the file name "14.gif".random number is generated when button "Generate" is clicked. how can i do that? images are in Resources folder inside the project folder.
I tried this but didnt work:

Public Class Form1

Dim images() As String
Dim RandClass As Random = New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    images = IO.Directory.GetFiles(My.Resources, "*.gif")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim R As Integer = RandClass.Next(0, images.Count)
    PictureBox1.Image = Image.FromFile(images(R))
End Sub

End Class

 
0
 

Nevermind guys :D found a solution to this question :D

 
0
 

Post your solution so others that have the same problem know what to do and mark this post as solved.

 
1
 

ok so I'm generating random number from 1 to 52(its a card game i'm doing) and then whichever number is generated the same image number is shown in pictureBox. I added ImageList tool from toolbox, I added images to ImageList. here is code:

Dim Generator As New Random
Dim randNum As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'generate random number

 randNum = Generator.Next(1, 52)

'show image of the card in pictureBox (picCard1)

         picCard1.Image = ImageList1.Images(randNum)  

End Sub

everytime button is clicked new image is shown in pictureBox and image is exactly the one i needed. For example: image of 2 spades called "2.gif" and when random number is 2 the image "2.gif" is shown!

Question Answered as of 1 Year Ago by HctiMitcH
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: