By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

random image selection in VB

P: 33
Hi
any idea how to randomly select an image/picture in vb please

thanks
Apr 23 '07 #1
Share this Question
Share on Google+
17 Replies


Expert 5K+
P: 8,434
Hi
any idea how to randomly select an image/picture in vb please
Can you be a bit more specific? What exactly do you mean by "select an image"? Give us some context to work with.

Also, what version of VB?
Apr 24 '07 #2

P: 33
thanks
Using VB 2005 express Edition, i'm writing a programme where a randomly selected picture (from 5), will be displayed at the click of a button.

Here's my coding:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x(4) As PictureBox
x(0).Equals(PictureBox1.Image)
x(1).Equals(PictureBox2.Image)
x(2).Equals(PictureBox3.Image)
x(3).Equals(PictureBox4.Image)
x(4).Equals(PictureBox5.Image)


Dim randomgenerator As New Random
Dim computerchoice As Integer

computerchoice = randomgenerator.Next(0, 5)
Apr 24 '07 #3

Expert 5K+
P: 8,434
Seems reasonable so far - so what's the problem?
Apr 24 '07 #4

SammyB
Expert 100+
P: 807
thanks
Using VB 2005 express Edition, i'm writing a programme where a randomly selected picture (from 5), will be displayed at the click of a button.

Here's my coding:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x(4) As PictureBox
x(0).Equals(PictureBox1.Image)
x(1).Equals(PictureBox2.Image)
x(2).Equals(PictureBox3.Image)
x(3).Equals(PictureBox4.Image)
x(4).Equals(PictureBox5.Image)


Dim randomgenerator As New Random
Dim computerchoice As Integer

computerchoice = randomgenerator.Next(0, 5)
Where do you want the image displayed? Wouldn't it be better to keep the images in an ImageList control? Why are you creating the x array?

If you have the images in PictureBoxes already, then you could just loop through them and set the Visibility to False except the randomly selected one.
Apr 24 '07 #5

P: 33
Seems reasonable so far - so what's the problem?

Thanks for helping Killer42, my problem is I can't progress I don't know how to progress
Apr 24 '07 #6

P: 33
Where do you want the image displayed? Wouldn't it be better to keep the images in an ImageList control? Why are you creating the x array?

If you have the images in PictureBoxes already, then you could just loop through them and set the Visibility to False except the randomly selected one.
Thanks for helping Guru, I'm gonna try it now, but I've never used ImageList control and not sure how to write the code to make the randomly selected visibility false
Apr 24 '07 #7

P: 33
Thanks for helping Guru, I'm gonna try it now, but I've never used ImageList control and not sure how to write the code to make the randomly selected visibility false
Sorry i meant the randomly seleceted visibility TRUE
Apr 24 '07 #8

P: 33
Hi here I still am with this programme.
I've got images in Resources and want to display randomly one in the picture box at the click of the button.
I've been really struggling with this for ages, help please anyone.
Here's my script and error message.
Thanks.


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim apple As String, banana As String, orange As String, pinnaple As String, mango As String
Dim m_stroptions As String() = New String() {apple, banana, orange, pinnaple, mango}

Dim m_blnUsed As Boolean() = _
New Boolean(m_stroptions.GetUpperBound(0)) {}


Dim m_intCount As Integer = 1
Dim m_strname As String


Dim stroutput As String
stroutput &= ".jpg"
stroutput = m_strname
stroutput = stroutput.Insert(0, _
System.Environment.CurrentDirectory & "\images\")


Dim getuniquerandomnumber As Integer
Dim objrandom As Random = New Random()
Dim intrandom As Integer
Do
intrandom = objrandom.Next(0, m_blnUsed.Length)
Loop Until m_blnUsed(intrandom) = False
m_blnUsed(intrandom) = True

intrandom = getuniquerandomnumber
m_strname = m_stroptions(intrandom)
Dim buildpathname() As String
Dim strpath As String = buildpathname()
PictureBox1.Image = Image.FromFile(strpath)


End Sub
End Class



Error 1 Number of indices is less than the number of dimensions of the indexed array.
Apr 29 '07 #9

P: 33
Hi here I still am with this programme.
I've got images in Resources and want to display randomly one in the picture box at the click of the button.
I've been really struggling with this for ages, help please anyone.
Here's my script and error message.
Thanks.


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim apple As String, banana As String, orange As String, pinnaple As String, mango As String
Dim m_stroptions As String() = New String() {apple, banana, orange, pinnaple, mango}

Dim m_blnUsed As Boolean() = _
New Boolean(m_stroptions.GetUpperBound(0)) {}


Dim m_intCount As Integer = 1
Dim m_strname As String


Dim stroutput As String
stroutput &= ".jpg"
stroutput = m_strname
stroutput = stroutput.Insert(0, _
System.Environment.CurrentDirectory & "\images\")


Dim getuniquerandomnumber As Integer
Dim objrandom As Random = New Random()
Dim intrandom As Integer
Do
intrandom = objrandom.Next(0, m_blnUsed.Length)
Loop Until m_blnUsed(intrandom) = False
m_blnUsed(intrandom) = True

intrandom = getuniquerandomnumber
m_strname = m_stroptions(intrandom)
Dim buildpathname() As String
Dim strpath As String = buildpathname()
PictureBox1.Image = Image.FromFile(strpath)


End Sub
End Class



Error 1 Number of indices is less than the number of dimensions
of the indexed array.
The error is on this line and area
Dim strpath As String = buildpathname()
Apr 29 '07 #10

Expert 5K+
P: 8,434
The error is on this line and area
Dim strpath As String = buildpathname()
buildpathname is an array. strPath isn't. How can you assign one to the other?
Apr 29 '07 #11

P: 33
thanks once again for all the help
i've got the code now without error but i did not achieve what i wanted: if i run it it will display always the first the first picture, so i don't think the random generator is working. to test it i proceeded by elimination and with this rest of the code, it still display the first picture
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3.  
  4.  
  5.         Dim intrandom As Integer
  6.  
  7.         Dim buildpathname() As String = {"C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\banana.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\mango.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\orange.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\pinnaple.jpg"}
  8.         Dim strpath As String = buildpathname(intrandom)
  9.         PictureBox1.Image = Image.FromFile(strpath)
  10.  
  11.     End Sub

here's my final code after correcting all the errors

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3.         Dim m_stroptions As String() = New String() {"apple", "banana", "orange", "pinnaple", "mango"}
  4.  
  5.         Dim m_blnUsed As Boolean() = _
  6.         New Boolean(m_stroptions.GetUpperBound(0)) {}
  7.  
  8.  
  9.         Dim m_intCount As Integer = 1
  10.         Dim stroutput As String
  11.         stroutput &= ".jpg"
  12.  
  13.         stroutput = stroutput.Insert(0, _
  14.         System.Environment.CurrentDirectory & "\images\")
  15.  
  16.  
  17.         Dim getuniquerandomnumber(5) As Integer
  18.         Dim objrandom As Random = New Random()
  19.         Dim intrandom As Integer
  20.         Do
  21.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  22.         Loop Until m_blnUsed(intrandom) = False
  23.         m_blnUsed(intrandom) = True
  24.  
  25.         intrandom = getuniquerandomnumber(5)
  26.         Dim m_strname As String = m_stroptions(intrandom)
  27.         Dim buildpathname() As String = {"C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\apple.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\banana.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\mango.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\orange.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\pinnaple.jpg"}
  28.         Dim strpath As String = buildpathname(intrandom)
  29.         PictureBox1.Image = Image.FromFile(strpath)
  30.  
  31.         m_intCount += 1
so these 2 previous codes give the same result when run which is displaying always the first image on this serie of paths

comme on guys, there should be a solution to randomly select an image from resources or imagelist and then display it in a picturebox
i've been working on this more than a week now and believe me i'm suffering as a spend the whole day on it
i'm normally not a quiter, but i'm thinking of giving up now
Apr 30 '07 #12

SammyB
Expert 100+
P: 807
Your code is a little screwy, but I think it will work. The major problem is that computer-generated random numbers are actually pseudo-random; that is, they generated the same set of "random" numbers each time, so since you create the random number generator in the click event, the same number is generated each time. You need to create the Random object in the form load event, so that each button push will generate the "next" random number. So, it should look like this:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private objRandom
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         objRandom = New Random()
  5.     End Sub
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
May 1 '07 #13

SammyB
Expert 100+
P: 807
Well, my code is wrong and yours is very screwy. First, we'll fix mine: the module-level definition of objRandom should be:
Expand|Select|Wrap|Line Numbers
  1.     Private objRandom As Random
Now, there are several problems in your code:
  • You initialize m_blnUsed in the button click, so it will always be false and your loop does nothing. It should be a module-level variable (that's why you named it m_) and initialized at least in the form load
  • stroutout & getuniquerandomnumber perform nothing useful
  • When all of m_blnUsed is false, it must be reinitialized before your loop or you'll just see the five images in random order, then you're in an infinite loop.
May 1 '07 #14

P: 33
hehehey, celebration time!!!
i'm there & went further on the program by your help, i can't thank you enough for all the support and for cheering me up
last thing (promise), in design after emptying the text in properties for a label, i can't see the label anymore and i need to see it to redesign it (set the text, modify its size); but it is nowhere to be seen; so how can i get to this label in design view
May 2 '07 #15

Expert 5K+
P: 8,434
In VB6, you can either select it in the dropdown list of elements at the top of the properties window, or drag a selection box around where you think it should be on the form.

In your version, I don't know - not familiar with the interface.
May 2 '07 #16

SammyB
Expert 100+
P: 807
It's still there, Killer! Now you can update your resume to VB.NET analyst.

As Killer says, use the drop-down at the top of the Properties window to select the label (you may have to cycle thru all of the labels to find the one you cannot see, then change its Location property to 0,0 so that it will be in the top left corner of your form and you can drag it where you want it.
May 2 '07 #17

P: 33
In VB6, you can either select it in the dropdown list of elements at the top of the properties window, or drag a selection box around where you think it should be on the form.

In your version, I don't know - not familiar with the interface.
thanks a lot killer42, i'm done now
May 2 '07 #18

Post your reply

Sign in to post your reply or Sign up for a free account.