473,387 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

random image selection in VB

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

thanks
Apr 23 '07 #1
17 14884
Killer42
8,435 Expert 8TB
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
Modibbo
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
Killer42
8,435 Expert 8TB
Seems reasonable so far - so what's the problem?
Apr 24 '07 #4
SammyB
807 Expert 512MB
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
Modibbo
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
Modibbo
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
Modibbo
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
Modibbo
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
Modibbo
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
Killer42
8,435 Expert 8TB
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
Modibbo
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
807 Expert 512MB
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
807 Expert 512MB
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
Modibbo
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
Killer42
8,435 Expert 8TB
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
807 Expert 512MB
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
Modibbo
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

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

Similar topics

4
by: kingofkolt | last post by:
I have a directory of images, called "random". In it are the following files: 1.gif 2.gif 3.gif 4.gif I use this script to choose a random image and display it:
6
by: Olly | last post by:
I've found a basic script, however I also need to add alt and title attributes as well, how would I go about doing this? Here's the script I found: Thanks <script language="JavaScript"> <!--...
5
by: Linda | last post by:
Greetings: I have found a random image script that I like. It is located here: http://www.javascriptcity.com/scripts/local/simage3.htm I'd like to edit this to have 4 different slots for...
5
by: Haydnw | last post by:
Hi, I have the code below as code-behind for a page which displays two images. My problem is with the second bit of code, commented as " 'Portfolio image section". Basically, the SQL query gets...
6
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional)...
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
1
by: eye ohh ewe | last post by:
Is there any simple coding that will take a random image from my images folder and display it when a user gets to the homepage Thanks in advance
3
by: fran7 | last post by:
Hi, I have this nice code that returns a random image database record. It works great. What I am trying to do now is to be able to get the "alt" description for the image from another field. If I...
2
by: Ron Poulton | last post by:
A recent project involved me obtaining a random image from the Web and using it in some specific fashion. I chose to use PHP to access Google Images for a random image URL. The image could be generic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.