Hi
any idea how to randomly select an image/picture in vb please
thanks
17 14884
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?
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)
Seems reasonable so far - so what's the problem?
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.
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
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
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
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.
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 ()
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?
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 - Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
-
-
-
Dim intrandom As Integer
-
-
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"}
-
Dim strpath As String = buildpathname(intrandom)
-
PictureBox1.Image = Image.FromFile(strpath)
-
-
End Sub
here's my final code after correcting all the errors - Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
-
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 stroutput As String
-
stroutput &= ".jpg"
-
-
stroutput = stroutput.Insert(0, _
-
System.Environment.CurrentDirectory & "\images\")
-
-
-
Dim getuniquerandomnumber(5) 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(5)
-
Dim m_strname As String = m_stroptions(intrandom)
-
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"}
-
Dim strpath As String = buildpathname(intrandom)
-
PictureBox1.Image = Image.FromFile(strpath)
-
-
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
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: - Public Class Form1
-
Private objRandom
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
objRandom = New Random()
-
End Sub
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Well, my code is wrong and yours is very screwy. First, we'll fix mine: the module-level definition of objRandom should be: - 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.
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
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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:
|
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">
<!--...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |